如何在宽度> = 300的情况下使用正则表达式IMG?

时间:2011-11-03 23:13:19

标签: php regex html-parsing

如何使用<img>获取所有width >= 300个?

我的标记/代码:

$images = <<<END
<img src="/data/img/201108031_023" width="300" height="400" />
<img src="/data/img/201108031_026" width="250" height="300" />
<img src="/data/img/201108031_028" width="400" height="300" />
<img src="/data/img/201108031_032" width="500" height="400" />
...
END;

我的尝试:

preg_match_all("/<img(.*?) \/>/",$images,$matches);
print_r($matches);

2 个答案:

答案 0 :(得分:6)

为此使用正则表达式并不是一个好主意。

这适用于您的特定示例,但它存在许多问题,因为正则表达式无法正确解析HTML:

"/<img[^>]*width=\"([3-9][0-9]{2}|[1-9][0-9]{3,})\"[^>]*>/"

查看在线工作:ideone

我建议您改为寻找HTML解析器。

相关

答案 1 :(得分:0)

"/<img[^>]*width=\"[0-9]*[3-9][0-9]{2}\"[^>]*\/>/"