0

Hi I am very new to this World of DOMDocument,Im still learning and looking for xpath query use in DOMDocument.The html sometimes changes so a preg_match is not a good idea. .I need to get the values from a html file.This is the part of html i want to get. I would be happy if you could help me..

 <?php  
    $doc = new DOMDocument();
    @$doc->loadHTML('<table cellspacing="0" cellpadding="0" align="center" class="results">
    <tr class="header" bgcolor="#0000FF">
    <td>
    </td>
    <td>Name/AKAs</td>
    <td>Age</td>
    <td>Location</td>
    <td>Possible Relatives</td>
    </tr>
    <tr>
    <td>1.</td>
        <td>
            <a class="LN" href=""><b>Iron, Man E</b></a>
        </td>
        <td align="center">54</td>
        <td>
            <a href="">Canada, AK</a><br />
            <a href="">California, AK</a><br />
        </td>
        <td>
        
        </td>
        <td>
            <a href="">View Details</a>
        </td>
    </tr>
              
    <tr><td>2.</td>
    <td>
    <a class="LN" href=""><b>Bat, Man E</b></a></td>
    <td align="center">26</td>
    <td>
    <a href="">Gotham, IA</a>
    <br /></td>
    <td>
    <a href=""> View Details</a></td></tr>
    </table>');
    $xpath = new DOMXPath($doc);
    $xquery = '//a[@class="LN"]';           
    $links = $xpath->query($xquery);   
    
    foreach ($links as $el) {
    echo strip_tags($doc->saveHTML($el)).'<br/>'; 
}

?>

How do I get the following value? I can only get Iron, Man E, and Bat, Man E

Iron, Man E | 54 | Canada, AK;California, AK
Bat, Man E | 26 | Gotham, IA
Syscall
  • 19,327
  • 10
  • 37
  • 52
Snippet
  • 1,522
  • 9
  • 31
  • 66

2 Answers2

2

My Answer is not about DomDocument Query but can solve your problem easily.

There is a Library named SIMPLEHTMLDOM ! You can do great things with it.

Example :

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('img') as $element) 
       echo $element->src . '<br>';

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>';

Full Documentation (Power of this Lib) is Here.

Hardik Thaker
  • 3,050
  • 1
  • 27
  • 37
0

Try this,

$xquery = '//a'; // you will get all anchor tags now
$links = $xpath->query($xquery);   

foreach ($links as $el) {
   echo strip_tags($doc->saveHTML($el)).'<br/>'; 
}

Try this to get in a single line,

$xpath = new DOMXPath($doc);
$xquery = '//tr[td[a]]';           
$links = $xpath->query($xquery);   
foreach ($links as $el) {
    echo strip_tags($doc->saveHTML($el)).'<br/>'; 
}
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • @Snippet test the above answer I've made changes in it. – Rohan Kumar Sep 06 '13 at 11:03
  • i tried your code and its almost correct. but i need to save tha Name,Age,and Location in different rows. – Snippet Sep 06 '13 at 11:16
  • @Snippet you can use two `xpath` like `one for containing class` and other for `not containing class` Read this http://stackoverflow.com/questions/14324179/php-xpath-contains-class-and-does-not-contain-class and http://stackoverflow.com/questions/4811949/php-domxpath-expression-to-select-a-tr-that-contains-an-input-with-certain-att – Rohan Kumar Sep 06 '13 at 12:05
  • is there a way i can get the 3 values and store it on an array like `array( array('Iron, Man E','54','Canada, AK;California, AK'));` – Snippet Sep 07 '13 at 07:30
  • @Snippet you can use `explode` function for this, Read http://php.net/manual/en/function.explode.php Use `explode` after `strip_tags` – Rohan Kumar Sep 07 '13 at 10:58