使用preg_match匹配IMG标签的SRC属性 [英] Matching SRC attribute of IMG tag using preg_match
问题描述
我试图运行preg_match从文章的第一个IMG标签中提取SRC属性(在这种情况下,存储在$ row-> introtext中).
I'm attempting to run preg_match to extract the SRC attribute from the first IMG tag in an article (in this case, stored in $row->introtext).
preg_match('/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\']*)/i', $row->introtext, $matches);
没有得到类似的东西
images/stories/otakuzoku1.jpg
来自
<img src="images/stories/otakuzoku1.jpg" border="0" alt="Inside Otakuzoku's store" />
我得到了
0
正则表达式应该是正确的,但是我不能说出为什么它似乎与border属性而不是src属性匹配.
The regex should be right, but I can't tell why it appears to be matching the border attribute and not the src attribute.
或者,如果您耐心阅读了这篇文章,而又没有直接跳到回复字段并输入使用HTML/XML解析器",那么可以推荐一本不错的教程,因为我很难找到一个完全适用于PHP 4.
Alternatively, if you've had the patience to read this far without skipping straight to the reply field and typing 'use a HTML/XML parser', can a good tutorial for one be recommended as I'm having trouble finding one at all that's applicable to PHP 4.
PHP 4.4.7
PHP 4.4.7
推荐答案
您的表达式不正确.试试:
Your expression is incorrect. Try:
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $row->introtext, $matches);
请注意,删除了img和src周围的括号以及其他一些清除措施.
Note the removal of brackets around img and src and some other cleanups.
这篇关于使用preg_match匹配IMG标签的SRC属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!