Change Innerhtml of a PHP Domelement

Change innerHTML of a php DOMElement

If you want to execute some javascript after the page loads, you need to insert it in the head of the document:

var script = document.creatElement('script');
script.src = "path to some script";
script.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);

or to use eval()

PHP DOMDocument replace DOMElement child with HTML string

If the HTML string can be parsed as XML, you can do this (after clearing the element of all child nodes):

$fragment = $doc->createDocumentFragment();
$fragment->appendXML($html_string);
$element->appendChild($fragment);

If $html_string cannot be parsed as XML, it will fail. If it does, you’ll have to use loadHTML(), which is less strict — but it will add elements around the fragment which you will have to strip.

Unlike PHP, Javascript has the innerHTML property which allows you to do this very easily. I needed something like it for a project so I extended PHP’s DOMElement to include Javascript-like innerHTML access.

With it you can access the innerHTML property and change it just as you would in Javascript:

echo $element->innerHTML;
$elem->innerHTML = '<a href="http://example.org">example</a>';

Source: http://www.keyvan.net/2012/11/php-domdocument-replace-domelement-child-with-html-string/

How to get innerHTML of DOMNode?

Compare this updated variant with PHP Manual User Note #89718:

<?php 
function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;

foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}

return $innerHTML;
}
?>

Example:

<?php 
$dom= new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($html_string);

$domTables = $dom->getElementsByTagName("table");

// Iterate over DOMNodeList (Implements Traversable)
foreach ($domTables as $table)
{
echo DOMinnerHTML($table);
}
?>

Getting the Inner HTML of a DomElement in PHP

$html = '';
foreach($parentElement->childNodes as $node) {
$html .= $dom->saveHTML($node);
}

CodePad.

DomDocument innerHTML does not work in simple php script

As there's no inneHTML-property, you can solve it by creating and appending a new DOMText node:

$index->getElementById('element-unique-id')
->appendChild(new DOMText('some text'));

See also The DOMText class and DOMText::__construct.

PHP DOMDocument: Get inner HTML of node

You need to have a root node to have a valid DOM document.

I suggest you to add a root node <div> to avoid to destroy a possibly existing one.

Finally, load the nodeValue of the rootNode or substr().

$body = "Some HTML with a <a href=\"http://stackoverflow.com\">http://stackoverflow.com</a>";
$body = '<div>'.$body.'</div>';

$dom = new DOMDocument;
$dom->loadHTML($body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

foreach ($dom->getElementsByTagName('a') as $node) {
$link_text = $node->ownerDocument->saveHTML($node->childNodes[0]);
$link_href = $node->getAttribute("href");
$link_node = $dom->createTextNode($link_href);

$node->parentNode->replaceChild($link_node, $node);
}

// or probably better :
$html = $dom->saveHTML() ;
$html = substr($html,5,-7); // remove <div>
var_dump($html); // "Some HTML with a http://stackoverflow.com"

This works is the input string is :

<p>Some HTML with a <a href=\"http://stackoverflow.com\">http://stackoverflow.com</a></p>

outputs :

<p>Some HTML with a http://stackoverflow.com</p>


Related Topics



Leave a reply



Submit