-3

I need remove all li which contain

<img src="/mapfiles/

For example I have:

<li class="g"><span>Text</span></li>
<li class="g"><span>Text<img src=\"/mapfiles/iconA.png\"></span></li>**- I need remove it**
<li class="g"><span>Text<img src=\"/mapfiles/iconB.png\"></span></li>**- I need remove it**
<li class="g"><span>Text</span></li>

I tried use this:

preg_replace('!<li class="g">(.*)<img src="/mapfiles/(.*)</li>!is', '', $content);

But Regex match first < li class="g"> and last < /li> so removes all < li>, how can I write regex to find the nearest < /li> ?

Marcin
  • 21
  • 4
  • 4
    Once again, we do not handle `HTML` with regex, we use HTML-parsers. Regex is not suitable for this. – Menno May 28 '13 at 14:16
  • See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags for a discussion on regexes vs (x)html. – Sysyphus May 28 '13 at 14:17
  • Once again the obligatory use a parser argument. Sometimes it is just not possible and regex is the only option. – SpongeBobPHPants Jun 30 '14 at 11:01

1 Answers1

4

THE PONY HE COMES...

Use a parser:

$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$remove = $xpath->query("//li[.//img[starts-with(@src,'/mapfiles/')]]");
foreach($remove as $r) $r->parentNode->removeChild($r);
$output = substr($dom->saveHTML($dom->getElementsByTagName('body')->item(0)),6,-7);
Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592