0

I have this HTML:

$html = 
<div class="content">
<table>..</table>
<table>..</table>
<table>..</table>
<table>..</table>
<table>..</table>
<table>..</table>
<table>..</table>
<table>..</table>
</div>

and php:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);

$body = $xpath->query('/table');

This echo's out all tables:

echo $doc->saveXml($body->item(0));
  1. My question is is it possible to save EACH table (with html tags) into array, so it would look like this:

    Array (
        [0] => < table> < /table> 
        [1] => < table> < /table>
        [2] => < table> < /table>
        .
        .
        .
        [n] => < table> < /table>
    )
    
  2. Is there a short way to echo out lets say 3rd table, something like this:

    echo $doc->saveXml($body->item(3));
    
Blue
  • 261
  • 1
  • 3
  • 14
  • possible duplicate of [innerHTML in PHP's DomDocument?](http://stackoverflow.com/questions/2087103/innerhtml-in-phps-domdocument) – Marc B Mar 07 '14 at 17:24

1 Answers1

1

I would recommend to do it with DomDocument itself:

foreach ($doc->getElementsByTagName('Table') as $item) {
    $array[] = $item->getNodePath();
    // or do any other process if you want
}

To get the specific item try this:

$node = $doc->getElementsByTagName('Table')->item(3)
//Example:
echo $node->nodeValue
Javad
  • 4,339
  • 3
  • 21
  • 36
  • getNodePath() retuns -> [0]=> string(19) "/html/body/table[1]", where can I find list of getNode..Alloptions ? is there possibility to say getNodeHtml() or something like this ? – Blue Mar 07 '14 at 17:46
  • Do you mean all child node? In the `foreach` you can say `$item->childNodes` which will give you the children of the node – Javad Mar 07 '14 at 17:54
  • 1
    If you want to convert the returned xpath to real xml string maybe this works: `SimpleXMLElement::asXML ($item->getNodePath());` – Javad Mar 07 '14 at 18:06