1

これは私が持っているコードであり、その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の例:

<html>

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

<body>

<div>one</div>

<div>two</div>

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

</body>

</html>

id = "linker"のhref値を取得したいのですが、getElementById()を試してみましたが、何も返されません。

4

2 に答える 2

2
$nodes = $doc->getElementsByTagName('title')->item(0)->nodeValue;
        $hrefValue = $doc->getElementById('linker')->getAttribute("href"); 
于 2012-07-10T07:29:26.570 に答える
-1
preg_match('/href=[\'"]?(.+?)[\'"]?.*?id=[\'"]?linker[\'"]?/si', $subject, $matches);
var_dump($matches);
于 2012-07-10T07:39:08.323 に答える