简体   繁体   English

获取具有特定ID的标签href值

[英]Get a tag href value with specific id

This is the code i have, and i want to get the href value for that id 这是我的代码,我想获取该id的href值

function file_get_contents_curl($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
}

    $str = 'someLink';
    $html = file_get_contents_curl($str);
        $doc = new DOMDocument();
        libxml_use_internal_errors(true);
        $doc->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $html);
        libxml_clear_errors();

        $nodes = $doc->getElementsByTagName('title');
        $hrefValue = $doc->getElementById('linker'); 

someLink html example: someLink html示例:

<html>

<head>
<title></title>
</head>

<body>

<div>one</div>

<div>two</div>

<div>
<a href="link" id="linker" />LINK HERE</a>
</div>

</body>

</html>

I want to get the href value for the id="linker".I try with getElementById() but it dosent return anything 我想获取id =“ linker”的href值。我尝试使用getElementById(),但是它不会返回任何内容

$nodes = $doc->getElementsByTagName('title')->item(0)->nodeValue;
        $hrefValue = $doc->getElementById('linker')->getAttribute("href"); 
preg_match('/href=[\'"]?(.+?)[\'"]?.*?id=[\'"]?linker[\'"]?/si', $subject, $matches);
var_dump($matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM