2013-01-03 100 views
0

可能重复:
Grabbing the href attribute of an A element忽略特定HTML类preg_match_all

我试图preg_match_all标签做具体的东西给他们,然而,有一个类型的,我想忽略preg_match_all。我考虑给img一个类,让preg_match_all忽略所有类被“忽略”的imgs。

换句话说,做一个preg_Match_all,如果您发现单词“忽略”,跳过它。

到目前为止我的代码是:

preg_match_all('/<img\s+.*?>/', $content, $matches); 

但detecs所有IMG,我卡在如何忽略那些与“忽略”,在它的词。

希望有人能帮我一把,谢谢你们。

+0

[使用DOM解析器](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662)。 – Gordon

+0

或尝试['xpath_match_all'](https://gist.github.com/1358174)''img [not(contains(@class,“ignored”))]' – Gordon

回答

1
$str = '<img src="path_to_image0"><img src="path_to_image1" class="ignored">'; 

preg_match_all("/<img(.*|!ignored)>/",$str,$matches); 

echo '<pre>'; 
print_r($matches[1]); 
echo '</pre>';