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('http://www.google.com/'); // 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
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;
1 comment (leave a comment)
yes, this is helpful.
by b.t. on June 2, 2011 at 12:05