Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Xpath选择<;a href="&引用&燃气轮机;价值_Php_Xpath - Fatal编程技术网

Php Xpath选择<;a href="&引用&燃气轮机;价值

Php Xpath选择<;a href="&引用&燃气轮机;价值,php,xpath,Php,Xpath,我试图从链接中选择值,但不知道如何解决此问题 <li id ="listing" class="listing premium first"> <header> <h2> <a href="#">value</a> </h2> </header> </li> 以下是我所有的代码: <?php $getURL = file_get_c

我试图从链接中选择值,但不知道如何解决此问题

<li id ="listing" class="listing premium first">
   <header>
      <h2>
        <a href="#">value</a>
      </h2>
   </header>
</li>
以下是我所有的代码:

<?php 
    $getURL = file_get_contents('realestate.com.kh/…'); 
    $dom = new DOMDocument(); 
    @$dom->loadHTML($getURL); 
    $xpath = new DOMXPath($dom); 
    /* echo $xpath->evaluate("normalize-space(substring-before(substring-after(//p[contains(‌​text(),'Property ID:')][1], 'Property ID:'), '–'))");*/ 
    echo $test = $xpath->evaluate('//li[contains(@class,"listing")]//header//h2//a')->item(0)->no‌​deValue;
?>

到底是什么不起作用?你有什么错误?你的问题很模糊。我在一个快速的phpfidle中运行了这个,您的代码运行得很好。您的代码示例没有提供如何在HTML中创建
$xpath
变量和/或加载。很可能这就是你的问题所在。我收到了这个erorr通知:试图在第9行的W:\Xampp\htdocs\X\index.php中获取非对象的属性。我的所有代码请阅读此参考以了解你得到的错误:我相信发生的是你认为从该站点获得的HTML,实际上不是您得到的HTML。我链接到的答案有一个使用xpath的示例,它与您正在做的事情相关。这将帮助您理解这个问题。这里还有一个链接,指向StackOverflow上关于PHP错误的全部参考--
<?php 
    $getURL = file_get_contents('realestate.com.kh/…'); 
    $dom = new DOMDocument(); 
    @$dom->loadHTML($getURL); 
    $xpath = new DOMXPath($dom); 
    /* echo $xpath->evaluate("normalize-space(substring-before(substring-after(//p[contains(‌​text(),'Property ID:')][1], 'Property ID:'), '–'))");*/ 
    echo $test = $xpath->evaluate('//li[contains(@class,"listing")]//header//h2//a')->item(0)->no‌​deValue;
?>