Search code examples
phpregexpreg-matchmultiline

preg match multiline text ends with a string


i have some html text with regular text in as well, i need to to be removed.

Example on text would be

<h3>title</h3>
this is some text
This some more

This more more, Continue reading.

I tried a million combinations, i just dont get the preg stuff, im using regex101 site to test every one, noone works of all my testing.

Tried like this (and million other combinations i feel)

<h3>.*?<\/h3>.*?{$\'Continue reading.\'}

I simply dont get the wierd buildup of preg, i mean i get i need start from <h3> and end at first string of "Continue reading." after the <h3>, but how to match this up over multiple lines is beyond me.

Can someone do and example and explain why? i read about multiline ect, but nothing works for me, i dont see it..

Thanks


Solution

  • To remove the entire string (or any string, in reality) you can use preg_replace and set the second parameter to ""; additionally you need to use the s modifier/flag to make the . selector match new lines (or use some other selector that matches anything).

    Regex

    /<h3>.*?Continue reading\./s
    /                            : Starting delimiter
     <h3>                        : Matches a literal <h3>
         .*?                     : Non-greedy match any character 0 or more times
            Continue reading     : Matches literally the text "Continue reading"
                            \.   : Match a literal period/full-stop
                              /  : Ending delimiter
                               s : Modifier/flag to make `.` match new lines
    

    Code Example

    In this example we've used "Erased" instead of "", to make it clear what's happened:

    $string = "<h3>title</h3>
    this is some text
    This some more
    
    This more more, Continue reading.";
    
    $string = preg_replace('/<h3>.*?Continue reading\./s', "Erased", $string);
    
    echo $string; // Output: Erased
    

    Use "" instead of "Erased" to completely remove the text:

    $string = preg_replace('/<h3>.*?Continue reading\./s', "", $string);