【发布时间】:2013-04-16 04:41:33
【问题描述】:
我是正则表达式的新手,我尝试了很多方法来获取锚标记 html 中的图像标记值 这是我的 html 表达式
<div class="smallSku" id="ctl00_ContentPlaceHolder1_smallImages">
<a title="" name="http://www.playg.in/productImages/med/PNC000051_PNC000051.jpg" href="http://www.playg.in/productImages/lrg/PNC000051_PNC000051.jpg" onclick="return showPic(this)" onmouseover="return showPic(this)">
<img border="0" alt="" src="http://www.playg.in/productImages/thmb/PNC000051_PNC000051.jpg"></a> <a title="PNC000051_PNC000051_1.jpg" name="http://www.playg.in/productImages/med/PNC000051_PNC000051_1.jpg" href="http://www.playg.in/productImages/lrg/PNC000051_PNC000051_1.jpg" onclick="return showPic(this)" onmouseover="return showPic(this)">
<img border="0" alt="PNC000051_PNC000051_1.jpg" src="http://www.playg.in/productImages/thmb/PNC000051_PNC000051_1.jpg"></a>
</div>
我只想返回图像标签的 src 值,我在“preg_match_all()”中尝试了一个匹配模式,模式是
"@<div[\s\S]class="smallSku"[\s\S]id="ctl00_ContentPlaceHolder1_smallImages"\><a title=\"\" name="[\w\W]" href="[\w\W]" onclick=\"[\w\W]" onmouseover="[\w\W]"\><img[\s\S]src="(.*)"[\s\S]></a><\/div>@"
请帮助我为此尝试了很多时间也尝试了此链接Match image tag not nested in an anchor tag using regular expression
【问题讨论】:
-
一个合适的 HTML 解析器可能比一个正则表达式为你提供更好的服务。
-
是的,但我需要一个正则表达式来代替那个..
-
@SunithSaga:为什么你需要正则表达式而不是DOM解析器? DOM 解析器在 100% 的时间里会比正则表达式做得更好。
-
不要使用正则表达式解析 HTML。您无法使用正则表达式可靠地解析 HTML,并且您将面临悲伤和挫败感。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。有关如何使用已经编写、测试和调试的 PHP 模块正确解析 HTML 的示例,请参阅 htmlparsing.com/php。
标签: php regex html-parsing