How can I match (PCRE) everything inbetween two tags?
I tried something like this:
<!--\s*LoginStart\s*-->(.*)<!--\s*LoginEnd\s*-->
But it didn't work out too well for me..
I'm kind of new to regular expressions, so I was hoping if someone would be kind enough to explain to me how I would accomplish this, if its even possible with regular expressions.
Thanks
$string = '<!-- LoginStart --><div id="stuff">text</div><!-- LoginEnds -->';
$regex = '#<!--\s*LoginStart\s*-->(.*?)<!--\s*LoginEnds\s*-->#s';
preg_match($regex, $string, $matches);
print_r($matches); // $matches[1] = <div id="stuff">text</div>
explanations:
(.*?) = non greedy match (match the first <!-- LoginEnds --> it finds
s = modifier in $regex (end of the variable) allows multiline matches
such as '<!-- LoginStart -->stuff
more stuff
<!-- LoginEnds -->'
PHP and regex? Here's some suggestions:
'/<!--\s*LoginStart\s*-->(.*)<!--\s*LoginEnd\s*-->/Us'
Might be better - the U
capitalized makes the regex non-greedy, which means it'll stop at the first <!--
that may work. But the important one is the s
, which tells the regex to match a newline with the .
character.
Depending on how certain you are on the capitalization, adding an i
at the end will make the regex search case-insensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With