Hi, I have a function that accepts a general HTML file and a general XPath expression. I want to extract a string containing the entire text including HTML tags, of the node matching the xPath expression. Here's a simplified example...

<?php
$inDocStg = "
    <html><body>
    <div>The best-laid<br> schemes o' <span>mice</span> an' men
        <img src='./mouse.gif'><br>
    </div>
    </body></html>
    ";
$xPathDom = new DOMDocument();
@$xPathDom->loadHTML( $inDocStg );
$xPath = new DOMXPath( $xPathDom );
$matches = $xPath->query( "//div" );  //DIV is just an example this could be any xPath exprn
echo $matches->item(0)->nodeValue;
?>

This produces (I'm looking at the generated HTML source - not the browser output)...

The best-laid schemes o' mice an' men

But what I want is ...

The best-laid<br> schemes o' <span>mice</span> an' men<img src='./mouse.gif'><br>

I.e. how can I get the entire string, tags and all.
Thanks.

    This is NOT using Xpath, but simple_html_dom.

    <?php
    
    include('simple_html_dom.php');
    
    $inDocStg = "
        <html><body>
        <div>The best-laid<br> schemes o' <span>mice</span> an' men
            <img src='./mouse.gif'><br>
        </div>
        </body></html>
        ";
    
    $html = new simple_html_dom();
    
    $html->load($inDocStg);
    
    echo $html->find('div', 0)->innertext;
      spiderplant0;10963428 wrote:

      I.e. how can I get the entire string, tags and all.
      Thanks.

      Nogdog's suggestion works excellent, at least for getting innerHTML, but I believe that code has problems when trying to remove childNodes. And personally I'd rather extend DomDocument to get cleaner and consistent syntax as well as split the function into get and set

      class DomDoc extends DomDocument {
      	function getInnerHTML(&$node) {
      		 ## if html parameter not specified, return the current contents of $node
      		$doc = new DOMDocument();
      		foreach ($node->childNodes as $child)
      		$doc->appendChild($doc->importNode($child, true));
      
      	return $doc->saveHTML();
      }
      
      function setInnerHTML(&$node, $html = false) {
      	## get rid of all current children
      	while ($node->hasChildNodes()) {
      		$node->removeChild($node->firstChild);
      	}
      
      	## if html is empty, we are done.
      	if($html == '') return;
      
      	## load up $html as DOM fragment, append it to our now-empty $node
      	$f = $this->createDocumentFragment();
      	$f->appendXML($html);
      	$node->appendChild( $f );
      }
      }
      

        Thanks for all the replys. I johnafam's suggestion. This worked:

            $inDocStg = "
                <html><body>
                <div>The best-laid<br> schemes o' <span>mice</span> an' men
                    <img src='./mouse.gif'><br>
                </div>
                <div>more</div>
                </body></html>
                ";
        
        //$xPathDom = new DOMDocument();
        $xPathDom = new DomDoc();
        @$xPathDom->loadHTML( $inDocStg );
        $xPath = new DOMXPath( $xPathDom );
        $matches = $xPath->query( "//div" );
        //echo $matches->item(0)->nodeValue;
        echo $xPathDom->getInnerHTML($matches->item(0));
        
          Write a Reply...