PHP DOM - 如何检索特定元素的节点值和 href


PHP DOM - How to retrieve nodeValue and href of a specific element

我有一个循环,我想推送第一个div 元素的 nodeValue 和 href。我是 DOM 的新手,所以我阅读了几篇文章并提出了以下代码。不幸的是,这是行不通的。希望有人能帮助我度过难关。提前感谢您的回复。干杯。马克

下面是$value的 html 结构(请参阅下面的循环以了解$value)

    <body>
        <div>
           <a href="myhreflink">this is my target</a>
           <a href="somehref">sometext</a>
        </div>
        <div></div>
        <div></div>
        <div></div>
    </body>

在我的循环下方:

 myarray = array();  
 /* $content is a DOMNodeList Object and $value is a DOMElement Object */
foreach ($content as $value){
    $firstDiv = $value->getElementsByTagName('div')[0];
    $firstA = $firstDiv->getElementsByTagName('a')[0];
    $val = $firstA->nodeValue;
    $link = $firstA->getAttribute('href');
    array_push($myarray, array('val'=>$val, 'href'=>$link));    
}

-> 下面是完整代码----------

<?php
header('Content-Type: text/html; charset=utf-8');
mysql_set_charset('utf8'); 
ini_set('display_errors', 1); error_reporting(E_ALL);
$liste = array();

    $url = 'myurl';
    $path = 'mypath'; 
    $titres = print_url_data($url, $path);
    foreach($titres as $value){
        array_push($liste, $value);
    }


// -> functions ------------------------------------------------------------
function print_url_data($url, $path){
    $content = get_url_data($url, $path);
    $tableau = array();
    foreach ($content as $value){
        $firstDiv = $value->getElementsByTagName('div')->item(0);
        $firstA = $firstDiv->getElementsByTagName('a')->item(0);
        $val = $firstA->nodeValue;
        $link = $firstA->getAttribute('href');
        array_push($myarray, array('val'=>$val, 'href'=>$link));  
    }
    return $tableau;
}
function get_url_data($url, $path){
    $xml_content = get_url($url);
    $dom = new DOMDocument();
    @$dom->loadHTML($xml_content);
    $xpath = new DomXPath($dom);
    $content_title = $xpath->query($path);
    return $content_title;
}
function get_url($url){
    $curl = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // browsers keep this blank.
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_REFERER, '[url=http://www.google.com]http://www.google.com[/url]');
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);

    $html = curl_exec($curl); 
    curl_close($curl);
    return $html; 
}
?>
这种类型的

数组取消引用在全新的 PHP 5.4 之前的 PHP 中无效。

// Can't do this unless running PHP5.4 (which seems unlikely)
// Actually without a way to test, I'm not sure that DOMDocument would even
// suppor this under PHP5.4
$firstDiv = $value->getElementsByTagName('div')[0];
$firstA = $firstDiv->getElementsByTagName('a')[0];

相反,您需要存储然后使用 item() 按索引检索值。

$firstDiv = $value->getElementsByTagName('div')->item(0);
$firstA = $firstDiv->getElementsByTagName('a')->item(0);

更新

我想我现在明白了 - 由于$content是一个 NodeList,你不想在foreach中迭代它,而是直接调用它getElementsByTagName()。 通过迭代它,您可以获得单个节点,而不是节点列表,并且您无法在我知道的单个节点上调用getElementsByTagName()

function print_url_data($url, $path){
    $content = get_url_data($url, $path);
    $tableau = array();
    // No need to loop. get nodes directly from $content   
    $firstDiv = $content->getElementsByTagName('div')->item(0);
    $firstA = $firstDiv->getElementsByTagName('a')->item(0);
    $val = $firstA->nodeValue;
    $link = $firstA->getAttribute('href');
    // this is not going to work since $myarray isn't initialized
    // you might have meant to use $tableau
    array_push($myarray, array('val'=>$val, 'href'=>$link));  
    return $tableau;
}