Frage

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));
    
War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top