当我想使用 XPath 打印评估表达式的结果时出现错误。
$url = $xpath->evaluate('//a/@href', $event); 回声 $url ;
我有这个错误:可捕获的致命错误:类 DOMNodeList 的对象无法转换为字符串
我的代码:
<?php
// Get the HTML Source Code
$url='http://www.parisbouge.com/events/2012/05/01/';
$source = file_get_contents($url);
// DOM document Creation
$doc = new DOMDocument;
$doc->loadHTML($source);
// DOM XPath Creation
$xpath = new DOMXPath($doc);
// Get all events
$events = $xpath->query('//li[@class="nom"]');
// Count number of events
printf('There is %d events<br />', $events->length);
// List all events
for($i = 0; $i < ($events->length); $i++) {
$event = $events->item($i);
$url = $xpath->evaluate('//a/@href', $event);
$nom = $xpath->evaluate('//a/text()', $event);
$lieu = $xpath->evaluate('../li[@class="lieu"]/a/text()', $event);
echo "Result : $url $nom $lieu <br/>";
}
?>