Combine everything between two tags with regular expressions?

How can I match (PCRE) everything between two tags?

I tried something like this:

<! - \ S * LoginStart \ S * β†’ </ l (*.); - \ S * LoginEnd \ S * β†’

But for me it didn’t work out too well.

I was kind of used to regular expressions, so I was hoping that if someone would be kind enough to explain to me how I accomplished this, if possible even with regular expressions.

thanks

+4
source share
3 answers
$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> 

explanation:

 (.*?) = 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 -->' 
+12
source

PHP and regex? Here are some suggestions:

 '/<!--\s*LoginStart\s*-->(.*)<!--\s*LoginEnd\s*-->/Us' 

Maybe it’s better - the uppercase U makes the regular expression not greedy, which means that it will stop at the first <!-- , which may work. But important is s , which tells the regular expression to match a new line with a character . .

Depending on how confident you are in capitalization, adding i to the end will do a case-insensitive search.

+1
source

I tried to answer Owen, but it could not be completed for conditions such as

<!-- LoginStart --><div id="stuff">text</div><!-- LoginEnds --> "DONT MIND THIS" <!-- LoginStart --><div id="stuff">text</div><!-- LoginEnds -->

This also includes the line "DONT MIND THIS", that is, it covers all the contents during the first <! - LoginStart β†’ and last <! - LoginEnds β†’ Tag

0
source

All Articles