正则表达式删除包含已定义字符串的所有div

时间:2013-05-28 14:11:05

标签: php regex html-lists

我需要删除包含

的所有li
<img src="/mapfiles/

例如我有:

<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>

我试过用这个:

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

但正则表达式首先匹配&lt; li class =“g”&gt;并且最后&lt; /立GT;所以删除所有&lt; li&gt;,如何编写正则表达式来查找最近的&lt; /立GT; ?

1 个答案:

答案 0 :(得分:4)

<强> THE PONY HE COMES...

使用解析器:

$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);