解析“href"中包含特定单词的所有链接标签 [英] Parse All Links That Contain A Specific Word In "href" Tag

查看:28
本文介绍了解析“href"中包含特定单词的所有链接标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
获取 A 元素的 href 属性

我需要解析包含某个单词的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆