Php 如何获取mp3作为扩展的链接
我有这个代码,它从一个网站上提取所有链接。如何编辑它,使其只提取以.mp3结尾的链接? 以下是代码:Php 如何获取mp3作为扩展的链接,php,hyperlink,extract,preg-match-all,Php,Hyperlink,Extract,Preg Match All,我有这个代码,它从一个网站上提取所有链接。如何编辑它,使其只提取以.mp3结尾的链接? 以下是代码: preg_match_all("/\<a.+?href=(\"|')(?!javascript:|#)(.+?)(\"|')/i", $html, $matches); preg\u match\u all(“/\更新: 一个很好的解决方案是与@zerkms一起使用,如评论中提到的: $doc = new DOMDocument(); $doc->loadHTML($yourHt
preg_match_all("/\<a.+?href=(\"|')(?!javascript:|#)(.+?)(\"|')/i", $html, $matches);
preg\u match\u all(“/\更新:
一个很好的解决方案是与@zerkms一起使用,如评论中提到的:
$doc = new DOMDocument();
$doc->loadHTML($yourHtml);
$xpath = new DOMXPath($doc);
// use the XPath function ends-with to select only those links which end with mp3
$links = $xpath->query('//a[ends-with(@href, ".mp3")]/@href');
原始答复:
我会使用DOM来实现这一点:
$doc = new DOMDocument();
$doc->loadHTML($yourHtml);
$links = array();
foreach($doc->getElementsByTagName('a') as $elem) {
if($elem->hasAttribute('href')
&& preg_match('/.*\.mp3$/i', $elem->getAttribute('href')) {
$links []= $elem->getAttribute('href');
}
}
var_dump($links);
更新:
一个很好的解决方案是与@zerkms一起使用,如评论中提到的:
$doc = new DOMDocument();
$doc->loadHTML($yourHtml);
$xpath = new DOMXPath($doc);
// use the XPath function ends-with to select only those links which end with mp3
$links = $xpath->query('//a[ends-with(@href, ".mp3")]/@href');
原始答复:
我会使用DOM来实现这一点:
$doc = new DOMDocument();
$doc->loadHTML($yourHtml);
$links = array();
foreach($doc->getElementsByTagName('a') as $elem) {
if($elem->hasAttribute('href')
&& preg_match('/.*\.mp3$/i', $elem->getAttribute('href')) {
$links []= $elem->getAttribute('href');
}
}
var_dump($links);
我更喜欢XPath,它用于解析XML/xHTML:
$DOM = new DOMDocument();
@$DOM->loadHTML($html); // use the @ to suppress warnings from invalid HTML
$XPath = new DOMXPath($DOM);
$links = array();
$link_nodes = $XPath->query('//a[contains(@href, ".mp3")]');
foreach($link_nodes as $link_node) {
$source = $link_nodes->getAttribute('href');
// do some extra work to make sure .mp3 is at the end of the string
$links[] = $source;
}
如果您使用的是XPath 2.0,则可以使用XPath函数替换contains()
。否则,您可能需要添加一个额外的条件,以确保.mp3
位于字符串的末尾。不过,这可能不是必需的。我更喜欢XPath,它用于解析XML/xHTML:
$DOM = new DOMDocument();
@$DOM->loadHTML($html); // use the @ to suppress warnings from invalid HTML
$XPath = new DOMXPath($DOM);
$links = array();
$link_nodes = $XPath->query('//a[contains(@href, ".mp3")]');
foreach($link_nodes as $link_node) {
$source = $link_nodes->getAttribute('href');
// do some extra work to make sure .mp3 is at the end of the string
$links[] = $source;
}
如果您使用的是XPath 2.0,您可以替换一个XPath函数contains()
。否则,您可能需要添加一个额外的条件,以确保.mp3
位于字符串的末尾。但可能不必这样做。您尝试过什么吗?使用DOM和以下XPath:/a[以(@href)结尾,“.mp3“)]
-我想这会容易得多:-)@zerkms XPath,以
结尾听起来比我的答案好得多!之前没有读过你的评论,$XPath=new DOMXPath($doc);$nodes=$XPath->query(“//a[以(@href,.mp3)结尾]);
-然后将这作为第二个代码添加到你的答案中:-)(没有测试,太懒了)@zerkms做到了。现在你试过什么了吗?使用DOM和下面的xpath://a[以(@href,.mp3”)]
-我想这会容易得多:-)@zerkms xpath,以
结尾听起来比我的答案好得多!在$xpath=new DOMXPath($doc);$nodes=$xpath->->->查询('//a[以
结尾)之前没有读过你的评论(@href,“.mp3”)]';
--将此作为第二个代码添加到您的答案中:-)(没有测试,太懒了atm)@zerkms做了。现在是cw了