1

我有一个循环,我想推送第一个 div 元素的第一个 a 元素的 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; 
}

?>
4

1 回答 1

2

这种类型的数组取消引用在全新 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;
}
于 2012-04-11T14:51:01.473 回答