Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

HTML parser in PHP

Tuesday, April 24th, 2018 by Servage

Are you ever struggling with HTML content in PHP? Do you need to parse a HTML source? Going through a HTML document in order to extract data might seem like a complicated, or even wrong, way of approaching a problem. Surely there must be another way… Well, sometimes other ways aren’t available, sometimes the quick and dirty HTML extraction is simply the easiest way to get things done. Therefore you might find yourself in situations where a working HTML parser for PHP would be a real help.

PHP Simple DOM Parser by C.C. Chen is a PHP class that does exactly what I described above. It’s available from Sourceforge. It can open an HTML document, parse the content, and make it available to you in your script, via simple Object methods. Take a look at the examples from the documentation below.

Loading HTML content

// Create a DOM object from a string
$html = str_get_html('Hello!');

// Create a DOM object from a URL
$html = file_get_html('');

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

Finding 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 all
which attribute id=foo $ret = $html->find(‘div[id=foo]‘); // Find all

with the id attribute $ret = $html->find(‘div[id]‘); // Find all element has attribute id $ret = $html->find(‘[id]‘);

Getting attributes

// 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;

Nice web-apps

HTML parser in PHP, 3.3 out of 5 based on 8 ratings
Categories: Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 comment (leave a comment)

yes, this is helpful.

Leave a comment

You must be logged in to post a comment.