D
D
Dmitry Evgrafovich2014-08-07 06:19:09
PHP
Dmitry Evgrafovich, 2014-08-07 06:19:09

How to get the entire content of an element using xpath?

How to get not just the text content of this or that element using the xpath request, but everything that it contains, including nested tags and their attributes, for example, if I access the block using the //div request, I get only its text and if there is a link in the block, then as a result I will get only the text content of the link, and not its href attribute, but I would like to get all the nested elements and their attributes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Evgrafovich, 2014-08-07
@Tantacula

Here is a working solution

<?php
function getInnerHTML($node)
{
    $children = $node->childNodes;
    foreach ($children as $child) 
    {
        $tmp_doc = new DOMDocument();
        $tmp_doc->appendChild($tmp_doc->importNode($child,true));
        $innerHTML .= $tmp_doc->saveHTML();
    }
    return $innerHTML;
}
?>

A
Artem Lisovsky, 2014-08-07
@torrie

In xpath, besides the textContent parameter, there are others, for example, innerHTML. Look in their direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question