【发布时间】:2015-09-24 12:38:38
【问题描述】:
我想从 html 网页中查找并打印所有 mp3 链接。
<a href="http://example1.com">Test 1</a>
<a href="http://example2.com/page.html">Test 2</a>
<a href="http://example3.com/link/file2.mp3">mp3 link 2</a>
<a href="http://example3.com/link/file3.mp3">mp3 link 3</a>
我现在使用 php 编写了 HTML 代码,我想在我的网页中打印两个 mp3 链接,所以请帮助我。我认为 DOM 可以在这里帮助我,但我不知道如何?您可以使用 DOM 或其他。谢谢你
让我们清楚-
$dom = new DomDocument();
$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $urls, $matches, PREG_SET_ORDER))
{ foreach($urls as $match)
{// $match[2] = link address
// $match[3] = link text}
}
这将打印所有链接,但我想打印一个具有 .mp3 远征的链接。我想现在很清楚了。所以请帮忙
【问题讨论】:
-
另请注意,我在这里使用 2 个 mp3 链接。但我会获取网页的整个 html,所以我不会知道他们有多少 mp3 链接的信息。所以请指导我检测网页中的所有 mp3 链接。这可能有助于找到link