C
C
Creel2018-07-19 14:59:36
Regular Expressions
Creel, 2018-07-19 14:59:36

How to write a regular expression for preg_match_all so that it doesn't cut the closing div?

How to write a regular expression with a check that if there is a nested div, then we skip both the opening and closing divs.

$content = '<div class="zzz">wwwww<div>zzz</div>yyy<div>qqqq</div>ppp</div>';
$pattern = "|<div class=\"zzz\">(.+?)<\/div>|";
$text = preg_match_all($pattern, $content, $return);
echo $return[1][0];

Here it cuts off incorrectly - how to count divas or their pairs?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question