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);
}
?>
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>
Getting the Inner HTML of a DomElement in PHP
$html = '';
foreach($parentElement->childNodes as $node) {
$html .= $dom->saveHTML($node);
}
CodePad.
How to get innerHTML of each element in a node list
Document.ofNode
should be Element.ofNode
, but the error message indicates that this line isn't present at all in the code producing the error. ofNode
also returns an option
, since not all nodes are elements, so you have to unwrap that as well. Something like this should work:
document
|> Document.querySelectorAll(".item")
|> NodeList.toArray
|> Array.map(Element.ofNode)
|> Array.map(Js.Option.getExn)
|> Array.map(Element.innerHTML)
|> Array.iter(Js.log);
Or to avoid a bit of unnecessary array allocation and mapping:
document
|> Document.querySelectorAll(".item")
|> NodeList.toArray
|> Array.iter(node =>
node
|> Element.ofNode
|> Js.Option.getExn
|> Element.innerHTML
|> Js.log
);
It also has to be said that direct DOM manipulation isn't a great fit for Reason, or any statically typed functional language for that matter, since the DOM is inherently a very dynamically typed and object-oriented API. NodeList.toArray
, for example, is required because a NodeList
is not an array but an "array-like", which supports only a few array operations.
How to get innerhtml of an element from PHP
PHP DOMDocument has already provided the function to retrieve content between your selectors. Here is how you do it
$div = $dochtml->getElementById('div2')->nodeValue;
So you don't need to make your own function.
How to get the html inside a $node rather than just the $nodeValue
You have to make an undocumented call on the node.
$node->c14n()
Will give you the HTML contained in $node
.
Crazy right? I lost some hair over that one.
http://php.net/manual/en/class.domnode.php#88441
Update
This will modify the html to conform to strict HTML. It is better to use
$html = $Node->ownerDocument->saveHTML( $Node );
Instead.
How to grab innerHTML from DOMNode
I think you could do better by treating them like actual nodes:
$good_node = $doc->createElement('bar');
foreach( $bad_node->childNodes as $kid )
{
$good_node->appendChild( $kid );
}
Related Topics
Error Message "Strict Standards: Only Variables Should Be Passed by Reference"
Stacking Multiple Ternary Operators in PHP
How to Get Parameters from a Url String
Sort Multidimensional Array by Multiple Columns
Strtotime() Doesn't Work With Dd/Mm/Yyyy Format
How to Sort Files by Date in PHP
Giving My Function Access to Outside Variable
How to Create Friendly Urls With .Htaccess
Best Way to Avoid Duplicate Entry into MySQL Database
Why Check Both Isset() and !Empty()
Ajax Request Callback Using Jquery
Pdoexception Sqlstate[Hy000] [2002] No Such File or Directory
How to Convert Json String to Array
How to Run PHP from Windows Command Line in Wampserver