• Resolved openbayou

    (@openbayou)


    How do I combine two preg_replace into one?

    preg_replace('/<img[^>]+./', "", $post->post_content)));  
    preg_replace('/(http|https):\/\/.*\/(.*)\.(jpg|gif|png)/', "", $post->post_content)));

    Thanks

    • This topic was modified 7 years, 2 months ago by openbayou.
Viewing 3 replies - 1 through 3 (of 3 total)
  • To simplify it’s:

    preg_replace ( $pattern , $replacement , $subject] )

    This function, “Searches subject for matches to pattern and replaces them with replacement.”, as described here:
    https://php.net/manual/en/function.preg-replace.php

    So, combining them would look something like this:
    $regex = “Your regex pattern goes here”;
    preg_replace($regex, “”, $post->post_content)));

    The pattern is a regex pattern if that wasn’t obvious. You may need an expert or to learn a bit about regex and then play with it until you get the desired result.
    I just found this tool, and bookmarked for myself for the next time I need a regex pattern. But, I haven’t used it yet. Maybe it’ll help though.
    https://regex101.com/

    Dion

    (@diondesigns)

    You can use arrays for the search and replace parameters. Here’s an example from the PHP preg_replace() documentation.

    https://secure.php.net/manual/en/function.preg-replace.php#example-5696

    Thread Starter openbayou

    (@openbayou)

    Thanks for the help.

    $imgpatterns = array ('/<img[^>]+./','/(http|https):\/\/.*\/(.*)\.(jpg|gif|png)/');
    preg_replace($imgpatterns, "", $post->post_content)));
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Combine two preg_replace into one’ is closed to new replies.