Extract Html Contents In A Certain Tag Without The Outer Tag In Php
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
Solution 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;
}
?>
Post a Comment for "Extract Html Contents In A Certain Tag Without The Outer Tag In Php"