Simple ScriptUsage Examples with PHP Simple HTML DOM Parser

If you want to parse HTML then let me tell you Regular Expressions is not the right way .There is a dream utility PHP Simple HTML DOM Parser for all the Web Developers which will function just perfect with both DOM and PHP because web developers can with ease find DOM elements by making use of PHP.The library of this parser is extensive with so many elements just like the early versions of JavaScript Frameworks and the Selector Engines. This parser has the capability to select content from various DOM nodes with PHP for analyzing any changes on the WebPages.

Functionality of PHP Simple HTML DOM Parser

This simple HTML DOM Parser has all the functions  that you need to manipulate HTML. With PHP Simple HTML DOM Parser you can extract all the contents from HTML in a single line , you can as well find tags on a HTML Web page with slectors just as the case with jQuery.One best thing about PHP Simple HTML DOM Parser is that it supports invalid HTML.

If you want to scrape data from a webpage or add or remove the various parts of a HTML document then Simple HTML DOM Parser is must download for you.

Here in this post on WDJ we will walk you through some great scripts that will help you make wonders with this PHP Simple HTML DOM Parser.

Quick Start

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