T
T
TillTill2016-04-02 16:23:14
PHP
TillTill, 2016-04-02 16:23:14

PHP+XPath, how to parse an html fragment without having to traverse all nested nodes in the resulting object to save the result?

I'm trying to get an html fragment containing a lot of child nodes. Example:

<body>
<table>...</table>
<div>
    <p>Sometext 1<br> Sometext2</p>
    <p>Sometext 3</p>
</div>
</body>

We need to get the contents of the div tag , preserving nested tags and text.
Implementation option in PHP+XPath bundle:
$xquery = '//div/node()';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query($xquery);

And then, in order to save the result, you need to loop through all the received nodes:
foreach($results as $key){
  $parsed_html .= $key->nodeValue;
}

How can you get the piece of html you want without having to loop through all the nested nodes in the resulting object?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2016-04-02
@ThunderCat

easier than regular IMHO. And faster. And eat less memory.
Well, or implode("",$results);

I
Ilya, 2016-04-02
@glebovgin

Here is an example of a working code, but I advise you to make it a separate method or function, at least to pass DOMNode into it:

$results = $xpath->query('//div[@class="example"]'); // тут путь до элемента, внутренности которого нужны
$innerHTML = ''; 
$children = $results->item(0)->childNodes;
foreach ($children as $child) 
  $innerHTML .= $results->item(0)->ownerDocument->saveHTML($child);

echo $innerHTML; // тут все внутренности

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question