我有一个循环,我想推送第一个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));
}
- &GT;下面是完整代码----------
<?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;
}
?>
答案 0 :(得分:1)
在全新的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;
}