Extract Html Contents in a Certain Tag without the

2019-07-26 22:14发布

问题:

I'd like to retrieve html code in a certain tag. I know DomDocument enables to do it. However, If I want to extract the contents without the outer tag, how can it be achieved?

For example,

$html = '<div><span>Hello world!</span><br><p>some other text</p></div>';    
$doc = new DOMDocument;
$doc->loadHTML($html);
echo $doc->saveXML($doc->getElementsByTagName('div')->item(0));

this will output,

<div>
    <span>Hello world!</span>
    <br>
    <p>some other text</p>
</div>

I want it without the outer div tag. I tried the node value but it strips all the tags.

$html = '<div><span>Hello world!</span><br><p>some other text</p></div>';    
$doc = new DOMDocument;
$doc->loadHTML($html);
$node = $doc->getElementsByTagName('div')->item(0);
echo $node->nodeValue;

Any ideas?

回答1:

All right, how about a PHP innerHTML implementation:

<?php 
$html = '<div><span>Hello world!</span><br><p>some other text</p></div>';    
$doc = new DOMDocument;
$doc->loadHTML($html);
$node = $doc->getElementsByTagName('div')->item(0);
echo DOMinnerHTML($node);

function DOMinnerHTML($element) 
{ 
    $innerHTML = ""; 
    $children = $element->childNodes; 
    foreach ($children as $child) 
    { 
        $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    } 
    return $innerHTML; 
} 
?>