-1

I need to capture several consecutive divs that I can recognize only with their start:

<div class="catched">
    something maybe with divs and more
</div>
<div class="catched">
    something else
</div>
<div class="catched">
    something else
</div>

However if I do preg_match('#<div class="catched">.+?<div class="catched"#i',$html,$match) it will only capture the first one. I already did that, I think it's with a non-capturing group but I don't find how to do and I don't remember how I did.

Paolo
  • 10,935
  • 6
  • 23
  • 45
Entretoize
  • 1,878
  • 3
  • 17
  • 29

1 Answers1

0

You may use a HTML parser instead:

<?php
$junk = <<<EOT
<div class="catched">
    something maybe with divs and more
</div>
<div class="catched">
    something else
</div>
EOT;

$dom = new DOMDocument();
$dom->loadHTML($junk);

$xpath = new DOMXPath($dom);
$divs = $xpath->query("*/div[@class='catched']");
foreach ($divs as $div) {
    echo $div->nodeValue;
}

?>

Which yields

something maybe with divs and more

something else


For a quick, dirty and error-prone solution, you could use
preg_match_ll('~<div class="catched">(?s)(.+?)</div>~');

See a demo on regex101.com.

Jan
  • 38,539
  • 8
  • 41
  • 69
  • 1
    The regex will not work because there are nesteds `DIV`s, from OP: `something maybe with divs and more` – Toto Apr 28 '20 at 13:29