简体   繁体   English

正则表达式匹配除具有特定属性的元素之外的所有元素

[英]Regex match all elements except ones with certain attribute

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. 但是我应该添加/修改它以忽略具有class='ignore'图像标签。

Cheers! 干杯!

Don't parse HTML with regex. 不要使用正则表达式解析HTML。

Find all images in html that have ignore class: 查找html中ignore类的所有图像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM