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://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 meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
innerHTML in PHP

Last accessed pages

  1. Simple Laravel MySQL CRUD Example (89)
  2. $_GET, $_POST and $_REQUEST Variables (24327)
  3. Simple Admin Login PHP Script (587)
  4. PuzzleImg - Script to Create Image Puzzle Game (1805)
  5. Editing, Changing XML - E4X (1002)

Popular pages this month

  1. Qwop (5845)
  2. Drag Racer V3 (2771)
  3. Flash Games - Free online Games (1439)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1246)
  5. Bubbles3 (1115)