【发布时间】:2013-07-22 03:29:40
【问题描述】:
<a href="http://newday.com/song.mp3">First Link</a>
<div id="right_song">
<div style="font-size:15px;"><b>Pitbull ft. Chris Brown - Pitbull feat. Chris Brown - International Love mp3</b></div>
<div style="clear:both;"></div>
<div style="float:left;">
<div style="float:left; height:27px; font-size:13px; padding-top:2px;">
<div style="float:left;">
<a href="http://secondurl.com/thisoneshouldonlyoutput" rel="nofollow" target="_blank" style="color:green;">Second Link</a></div>';
我想使用 pregmatch_all 从此 html 中取出第二个链接。我当前的正则表达式如下所示:
preg_match_all("/\<a.+?href=(\"|')(?!javascript:|#)(.+?)\.mp3(\"|')/i", $html, $urlMatches);
这很好,我得到了两个链接输出,但我只希望输出第二个没有 .mp3 扩展名的链接。请帮帮我
【问题讨论】:
-
我们不使用正则表达式解析 html
-
使用 HTML DOM 解析器解析 HTML DOM - simplehtmldom.sourceforge.net
-
我知道我不应该使用正则表达式,但我现在必须这样做。有什么帮助吗?
-
不要使用正则表达式解析 HTML。您无法使用正则表达式可靠地解析 HTML,并且您将面临悲伤和挫败感。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。有关如何使用已经编写、测试和调试的 PHP 模块正确解析 HTML 的示例,请参阅 htmlparsing.com/php。
标签: php regex html-parsing preg-match-all