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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
innerHTML in PHP

Last accessed pages

  1. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28165)
  2. Legendary Escape (138)
  3. Clock House Santa (38)
  4. Escape Games (1629)
  5. Spring Garden Cleanup (23)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (45791)
  2. Read Excel file data in PHP - PhpExcelReader (34450)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (34339)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (30919)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28165)