0

Sorry, if this has been asked and answered before, but I cannot seem to find an answer to this.

Since regex is not my strong side I turn to you, SO. I'm stuck with a regex that needs to find all the image tags in text except ones that have a certain attribute and value. At this point I am not even sure if that is doable, if it is - any help is appreciated.

/<img.*?\/(img)?>/si

This is what I use to match all of the image tags - simple and straight forward. But what should I add/modify for it to ignore image tags with class='ignore' for example.

Cheers!

Eliah John
  • 77
  • 6
  • This doesn't really sound like a good match for RegEx, you should use a HTML parser instead. – sQVe Nov 08 '13 at 10:17
  • Please have a look at the [first question that comes up in the related section](http://stackoverflow.com/q/1732348/112968) – knittl Nov 08 '13 at 10:17

1 Answers1

2

Don't parse HTML with regex.

Find all images in html that have ignore class:

$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//img[contains(@class, "ignore")]') as $img) {
    // do something with $img
}
echo $dom->saveHTML();
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107