Home PHP JavaScript CSS WordPress APIs .htaccess Other How-To Useful Scripts What I Recommend
Posted on by Aleksandar Gichevski ()
Today I will write about one great php parser that made my coding easier last week. Last Wednesday I had to scrape some html code, extract all necessary data that was around 50 different values positioned all over html and store in php array for further use. When I started creating my script I thought how it will be very easy if this was coded in JavaScript/jQuery instead PHP, we would have used JS predefined functions and get the data based on ids, class attributes and other info... I did quick search on internet to check if there is some good php parser that would do exact what is possible to do with JS and I found PHP Simple HTML DOM Parser.

What is PHP Simple HTML DOM Parser?

This is HTML DOM Parser written in PHP that gives you all needed functions to easily manipulate with any HTML.
  • Supports invalid HTML.
  • Find tags on an HTML page with selectors just like jQuery.
  • Extract contents from HTML in a single line.

What can you do with PHP Simple HTML DOM Parser?

With HTML DOM Parser you can do everything that you can with JavaScript when working with HTML like:
  • Extract HTML content directly from URL, string or HTML file, DOM element can be created on 2 ways, Normal and Object-oriented
  • Get HTML elements by tag, attribute or by using descendant and nested selectors
  • Modify HTML elements - Gee/Set/Remove attributes and Insert/Append/Remove elements
and more...

Few Examples

Let me show you with few examples what this powerful script can do... Create DOM Element Quick Way:
Hello!');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
Object Oriented Way:
load('Hello!');

// Load HTML from a URL 
$html->load_file('http://www.google.com/');

// Load HTML from a HTML file 
$html->load_file('test.htm');
Find HTML Elements
// Find all anchors, returns a array of element objects
$ret = $html->find('a');

// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);

// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1); 

// Find all 
with the id attribute $ret = $html->find('div[id]'); // Find all
which attribute id=foo $ret = $html->find('div[id=foo]'); // Find all element which id=foo $ret = $html->find('#foo'); // Find all element which class=foo $ret = $html->find('.foo'); // Find all element has attribute id $ret = $html->find('*[id]'); // Find all anchors and images $ret = $html->find('a, img'); // Find all anchors and images with the "title" attribute $ret = $html->find('a[title], img[title]'); // Find all
  • in
      $es = $html->find('ul li'); // Find Nested
      tags $es = $html->find('div div div'); // Find all in which class=hello $es = $html->find('table.hello td'); // Find all td tags with attribite align=center in table tags $es = $html->find(''table td[align=center]'); Modify HTML Element
      // Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
      $value = $e->href;
      
      // Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
      $e->href = 'my link';
      
      // Remove a attribute, set it's value as null! 
      $e->href = null;
      
      // Determine whether a attribute exist? 
      if(isset($e->href)) 
              echo 'href exist!';
      
      // Extract contents from HTML 
      echo $html->plaintext;
      
      // Wrap a element
      $e->outertext = '
      ' . $e->outertext . '
      '; // Remove a element, set it's outertext as an empty string $e->outertext = ''; // Append a element $e->outertext = $e->outertext . '
      foo
      '; // Insert a element $e->outertext = '
      foo
      ' . $e->outertext;

      Download

      To start using this php parser you can download it from here. Also to see the full potential and all available functions of this parser read Online Document.

      Other Posts You Might Like

      Get a fast, free website audit


      Enter your URL below to get full in-depth SEO report and tips.

      Useful Scripts

      Most Popular Posts

      Recent Posts

      June 14th, 2014
      Website Optimization Using Gzip Compression

      June 10th, 2014
      Google Search AutoComplete API

      May 14th, 2014
      What is a Tag Cloud and How to Calculate it by Formula

      March 27th, 2014
      Error writing file /tmp (errcode 28) Solved!

      February 15th, 2014
      Fixed CSS & HTML Navigation Bar

      February 9th, 2014
      Benchmark Your Server (CPU, File IO, MySQL) with SysBench

      February 8th, 2014
      JavaScript Array Basics

      January 29th, 2014
      JVZoo IPN API in PHP

      January 25th, 2014
      Payoneer Debit Card for Freelancers to receive money online

      January 24th, 2014
      How HTML Color Codes are generated?

      Read Latest Posts directly on Facebook

      Archive