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: http://www.marplo.net/jocuri/</div>
 <div id="dv2"><p class="cls">Free PHP Course: <a href="http://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="http://coursesweb.net/php-mysql/" title="PHP Course">CoursesWeb.net</a></p>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
innerHTML in PHP

Last accessed pages

  1. The Rise Of Atlantis (5168)
  2. Volume and Surface Area Calculator for 3D objects (5928)
  3. Dog Mahjong (3485)
  4. Bloons Tower Defense 4 (1173)
  5. Read Excel file data in PHP - PhpExcelReader (41452)

Popular pages this month

  1. Qwop (4763)
  2. Bubbles3 (4405)
  3. Butterfly Kyodai (3134)
  4. Zuma Deluxe (2904)
  5. Backgammon (1486)