The
innerHTML() function presented in this page it is the equivalent of the
innerHTML property from JavaScript. This function can be used in PHP to get the HTML content from a HTML element within a DOMDocument object.
Code of innerHTML()
// returns a string with the HTML content from a DOMDocument node element ($elm)
function innerHTML(DOMNode $elm) {
$innerHTML = '';
$children = $elm->childNodes;
foreach($children as $child) {
$innerHTML .= $elm->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
- Example usage of innerHTML().
<?php
// returns a string with the HTML content from a DOMDocument node element ($elm)
function innerHTML(DOMNode $elm) {
$innerHTML = '';
$children = $elm->childNodes;
foreach($children as $child) {
$innerHTML .= $elm->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
// string with HTML content
$strhtml = '<div id="dv1">Flash Games: https://marplo.net/jocuri/</div>
<div id="dv2"><p class="cls">Free PHP Course: <a href="https://coursesweb.net/php-mysql/" title="PHP Course">CoursesWeb.net</a></p></div>';
// create the DOMDocument object, and load HTML from $strhtml
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
// get the element with id="dv2"
$dv2 = $dochtml->getElementById('dv2');
// uses innerHTML() to get the HTML content from $dv2
$cnt_dv2 = innerHTML($dv2);
// output the HTML content
echo $cnt_dv2;
?>
- Output:
<p class="cls">Free PHP Course: <a href="https://coursesweb.net/php-mysql/" title="PHP Course">CoursesWeb.net</a></p>
Daily Test with Code Example
HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area><img src="image.jpg" usemap="#map1">
<map name="map1">
<area shape="rect" coords="9, 120, 56, 149" href="#">
<area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position#id {
overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseoutdocument.getElementById("id").onmouseover = function(){
document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POSTif(isset($_GET["id"])) {
echo $_GET["id"];
}