2

I am trying to select value from a link but, don't know how to solve this problem.

<li id ="listing" class="listing premium first">
   <header>
      <h2>
        <a href="#">value</a>
      </h2>
   </header>
</li>

I tried with this code and didn't work.

echo $test = $xpath->evaluate('//li[contains(@class,"listing")]//header//h2//a')->item(0)->nodeValue;

Here's all of my code:

<?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;
?>

And here's the actual error I get:

Notice: Trying to get property of non-object in W:\Xampp\htdocs\X\index.php on line 9

Is something wrong with this code? I don't know how to make this work. pls help me anyone. I thanks u all.

Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
july77
  • 673
  • 2
  • 8
  • 24
  • What is not working, exactly? What error do you get? Your question is vague. I ran this in a quick PhpFiddle and your code works just fine. Your code example does not provide how you are creating the `$xpath` variable and/or loading in the HTML. Chances are that is where your problem lies. – Dmitriy Khaykin Nov 13 '15 at 11:13
  • I got this erorr Notice: Trying to get property of non-object in W:\Xampp\htdocs\X\index.php on line 9 – july77 Nov 13 '15 at 11:23
  • all of my code 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)->nodeValue; – july77 Nov 13 '15 at 11:23
  • Please read this reference to understand the error you're getting: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/26572398#26572398 What I believe is happening is that the HTML you think you're getting from the site, is not actually the HTML you are getting. The answer that I linked to has an example using xpath which is relevant to what you're doing. It will help you understand the problem. – Dmitriy Khaykin Nov 13 '15 at 11:51
  • Also here is the link to the entire reference on StackOverflow about PHP errors in general -- http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/ – Dmitriy Khaykin Nov 13 '15 at 11:53
  • have you tried `//li[contains(@class,"listing")]//a/@href` ? – Learner Nov 14 '15 at 04:37

0 Answers0