PHP Simple HTML DOM is a HTML DOM parser written in PHP5+ . This class let you manipulate HTML in a very easy way, find tags on an HTML page with selectors just like jQuery.
- PHP Simple HTML DOM 1.5.


API Reference

Helper functions:
DOM methods & properties:
Element methods & properties:
DOM traversing:
You can also call methods with W3C STANDARD camel naming convertions.
1. Find all links, and their text, in a page from a URL:

// Create DOM from URL or file
$html = file_get_html('http://coursesweb/');

// Find all links, and their text
foreach($html->find('a') as $elm) {
  echo $elm->href .' ('.$elm->plaintext. ')<br/>';
html/ (HTML)
css/ (CSS)
javascript/ (JavaScript)
php-mysql/ (PHP-MySQL)
ajax/ (AJAX)
flash/ (Flash - ActionScript)
ex/contact (Contact)

2. Find all images with a specified class attribute, in a HTML content defined in PHP script:

// Create a DOM object from a string
$html = str_get_html('<div><img src="image1.jpg" alt="Img1" class="cls" /><br/>
 <img src="image2.png" alt="Img2" /></div><p>Some text</p>
 <img src="image3.gif" alt="Img3" class="cls" />');

// Find all images with class="cls"
foreach($html->find('img.cls') as $elm) {
  echo $elm->src. '<br/>';

3. Get the id of the first LI in a UL list, change its text, and output the new content

// Create a DOM object from a string
$html = str_get_html('<nav><ul>
 <li id="idli1" class="cls">List 1</li><li>List 2</li><li class="cls">List 3</li>

// Get the id of the first LI in UL, and change its content
$idli = $html->find('li', 0)->id;
if($idli) echo 'First LI id: '. $idli;
$html->find('ul li', 0)->innertext = '<b>PHP Simple HTML DOM</b>';
echo $html;
Result this HTML code:
First LI id: idli1
<li id="idli1" class="cls"><b>PHP Simple HTML DOM</b></li>
<li>List 2</li>
<li class="cls">List 3</li>

4. Using a callback function, that is applied to each element in DOM (changes the class attribute).

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

// Write a function with parameter "$elm"
function changeCls($elm) {
  // if LI with class="cls", change the class
  if ($elm->tag=='li' && $elm->class=='cls') {
    $elm->setAttribute('class', 'class_2');
echo $html;

- In the archive with "PHP Simple HTML DOM" class you'll find more examples, and documentation (in directory accessed from server).
PHP Simple HTML DOM Web Site.

Daily Test with Code Example

Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert(""); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
PHP Simple HTML DOM Parser

Last accessed pages

  1. Creating XML data - E4X in ActionScript 3 (2529)
  2. Get and Modify content of an Iframe (17024)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (77872)
  4. JavaScript code and PHP (24203)
  5. PHP getElementById and getElementsByTagName (27747)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (2498)
  2. Contact page - CoursesWeb (2394)
  3. Tabs effect with CSS (2393)
  4. Insert, Select and Update NULL value in MySQL (1117)
  5. PHP getElementById and getElementsByTagName (991)