0

I need the divs contents where the divs classes are red or green:

$s = '<div class="red">111</div> xxx <div class="green">222</div>';

preg_match_all('!<div class="(red|green)">(.*?)</div>!', $s, $tomb);

This works fine, but I don't need the first backreference, only the second, the div content. I tried with assertion:

preg_match_all('!<div class="(?=red|green)">(.*?)</div>!', $s, $tomb);

but it does not match anything.

How do I solve this problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
Lay András
  • 795
  • 2
  • 9
  • 14
  • 1
    https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?rq=1 – AbraCadaver May 16 '19 at 15:22
  • Regex is not really suited for html. I advice you to use simplexml or domdocument to work with html.. you can use xpath for more easy selections – Raymond Nijland May 16 '19 at 15:23

0 Answers0