php

PHP Simple HTML DOM Parser


URL : http://simplehtmldom.sourceforge.net/
  • A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
  • Require PHP 5+.
  • Supports invalid HTML.
  • Find tags on an HTML page with selectors just like jQuery.
  • Extract contents from HTML in a single line.

Download & Documents

// Create DOM from URL or file
$html = file_get_html(‘http://www.google.com/’);

// Find all images
foreach($html->find(‘img’) as $element)
echo $element->src . ‘<br>’;

// Find all links
foreach($html->find(‘a’) as $element)
echo $element->href . ‘<br>’;

===================================================

// Create DOM from string
$html = str_get_html(‘<div id=”hello”>Hello</div><div id=”world”>World</div>’);

$html->find(‘div’, 1)->class = ‘bar’;

$html->find(‘div[id=hello]’, 0)->innertext = ‘foo’;

echo $html; // Output: <div id=”hello”>foo</div><div id=”world” class=”bar”>World</div>

=============================================
// Dump contents (without tags) from HTML
echo file_get_html(‘http://www.google.com/&#8217;)->plaintext;

===========================================

// Create DOM from URL
$html = file_get_html(‘http://slashdot.org/&#8217;);

// Find all article blocks
foreach($html->find(‘div.article’) as $article) {
$item[‘title’]     = $article->find(‘div.title’, 0)->plaintext;
$item[‘intro’]    = $article->find(‘div.intro’, 0)->plaintext;
$item[‘details’] = $article->find(‘div.details’, 0)->plaintext;
$articles[] = $item;
}

print_r($articles);

==========================================

Feedback

Author: S.C. Chen (me578022@gmail.com)
Original idea is from Jose Solorzano’s HTML Parser for PHP 4.
Contributions by: Yousuke Kumakura (Attribute Filters)
==============================================================

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s