1

I use this code to get all the link in a html page and it worked fine.

<?php
    $html = file_get_contents('http://realestate.com.kh/real-estate-for-sale-in/all/');

    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    // grab all the on the page
    $xpath = new DOMXPath($dom);
    $hrefs = $xpath->evaluate("/html/body//a");

    for ($i = 0; $i < $hrefs->length; $i++) {
           $href = $hrefs->item($i);
           $url = $href->getAttribute('href');
           echo $url.'<br />';
    }
?>

But I want to get only specific link from div or class just like this

<ul class="menu">
 <li><a href="#">product1</li>
 <li><a href="#">product2</li>
 <li><a href="#">product3</li>
 <li><a href="#">product4</li>
</ul>

So how can I give the class to the above code so that I can get only link in menu class.

Thank you so much for help me solve this.

july77
  • 673
  • 2
  • 8
  • 24
  • 2
    Possible duplicate of [PHP DOM Xpath - search for class name](http://stackoverflow.com/questions/8680721/php-dom-xpath-search-for-class-name) – Mikel Bitson Nov 03 '15 at 15:24

1 Answers1

0

You should be able to do

$xpath->query('/html/body/ul[contains(@class,"menu")]/a');

SEE: PHP DOM Xpath - search for class name

Community
  • 1
  • 1
Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • Have you var_dumped the results to find out why? Is there an error? Did you read through the xpath->query docs to confirm you're using everything correctly? Try $xpath->query('//ul[contains(@class,"menu")]/a'); – Mikel Bitson Nov 03 '15 at 17:19
  • object(DOMNodeList)#3 (1) { ["length"]=> int(0) } – july77 Nov 03 '15 at 17:38
  • So it's not finding the ul with the class menu. Try var_dump($dom->saveHTML()); and add the output to your initial question. This will ensure that the dump has the menu class. If it does, you could try a different method, like Zend_Dom_Query, which supports CSS selectors: http://stackoverflow.com/questions/6366351/getting-dom-elements-by-classname – Mikel Bitson Nov 03 '15 at 17:44
  • string(387) " product1 product2 product3 product4 " – july77 Nov 03 '15 at 17:46
  • That string is what you get when you var_dump the $dom's HTML? This is why the xpath query is failing, it has no XML (HTML) nodes to query. If that is the string you're trying to load into the dom, why not just explode it on spaces and run with it? – Mikel Bitson Nov 03 '15 at 17:49
  • yes.How about $hrefs = $xpath->evaluate("/html/body//a"); – july77 Nov 03 '15 at 17:51
  • The $xpath->evaluate will never return the nodes you're looking for because the $xpath variable operates on the $dom variable, and the $dom variable has no html? Right? var_dump($dom->saveHTML()) produced no html? Try var_dump($html);. EDIT: Why are you using a @ when loading the html? This could be suppressing important warnings that you need to debug this. – Mikel Bitson Nov 03 '15 at 17:54