解析"href"中包含特定单词的所有链接.标签 [英] Parse All Links That Contain A Specific Word In "href" Tag
本文介绍了解析"href"中包含特定单词的所有链接.标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
抓取A元素的href属性
Possible Duplicate:
Grabbing the href attribute of an A element
我需要解析包含某些单词的HTML文档的所有链接(总是不同的).
I need to parse all links of an HTML document that contain some word (it's always different).
示例:
<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>
我只需要带有"href =/link:...."的链接,什么是最好的方法?
I only need the links with "href=/link: ...." what's the best way to go for it?
$html = "SOME HTLM ";
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
foreach ($urls as $url)
{
echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
echo "<hr><br>";
}
在此示例中,显示了所有链接,我需要特定的链接.
In this example all links are shown, I need specific links.
推荐答案
使用条件.
<?php
$lookfor='/link:';
foreach ($urls as $url){
if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
echo "<br> ".$url->getAttribute('href')." , ".$url->getAttribute('title');
echo "<hr><br>";
}
}
?>
这篇关于解析"href"中包含特定单词的所有链接.标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文