PHP Simple HTML DOM Parser: Editing HTML Elements in PHP
Vojislav JanjicGetting Started with PHP Simple HTML DOM Parser
- Load HTML from an URL
- Load HTML from a string
- Load HTML from file
<?php
// Create a DOM object; You must do this for each way
$html = new simple_html_dom();
// Load HTML from an URL
$html->load_file('http://www.mydomain.com/');
// Load HTML from a string
$html->load('<html><body>Hello world!</body></html>');
// Load HTML from a HTML file
$html->load_file('path/to/file/test.html');
?>
Find HTML Elements with PHP Simple HTML DOM Parser
<?php
// Find all elements by tag name e.g. a. Note that it returns an array with object elements.
$a = $html->find('a');
// Find (N)th element, where the first element is 0. It returns object or null if not found.
$a = $html->find('a', 0);
// Find the element where the id is equal to a certain value e.g. div with id="main"
$main = $html->find('div[id=main]',0);
// Find all elements which have the id attribute. E.g. find all divs with attribute id.
$divs = $html->find('div[id]');
// Find all elements that have attribute id
$divs = $html->find('[id]');
?>
<?php
// Find all elements where id=container. Have in mind that two elements with the same ids is not valid HTML.
$ret = $html->find('#container');
// Find all elements where class=foo
$ret = $html->find('.foo');
// You can also find two or more elements by tag name
$ret = $html->find('a, img');
// Find two or more elements by tag name where certain attribute value exists e.g. find all anchors and images with the attribute title.
$ret = $html->find('a[title], img[title]');
?>
<?php
// Find all <li> in <ul>
$ret = $html->find('ul li');
//find all <li> with class="selected" in <ul>
$ret = $html->find('ul li.selected');
?>
Parent, child and sibling elements can be selected by using built-in functions:
<?php
// returns the parent of a DOM element
$e->parent;
// returns element children in an array
$e->children;
// returns a specified child, by number, starting from zero. If child is not found, null is returned.
$e->children(0);
// returns first child of an element, or null if not found.
$e->first_child ();
// returns last child of an element
$e->last _child ();
// returns previous sibling of an element
$e->prev_sibling ();
//returns next sibling of an element
$e->next_sibling ();
?>
Use Attribute Operators
[attribute]
- Select HTML DOM elements which have a certain attribute[attribute=value]
- Select all elements which have the specified attribute with a certain value[attribute!=value]
- Select all elements which don't have the specified attribute with a certain value[attribute^=value]
- Select all elements with the specified attribute whose value begins with the specified value[attribute$=value]
- Select all elements with the specified attribute whose value ends with the specified value[attribute*=value]
- Select all elements with the specified attribute whose value contains the specified value
Access DOM Element Attributes with PHP Simple HTML DOM Parser
<?php
// In this example we select href attribute of an anchor. If the attribute is a non-value attribute (e.g. checked, selected…), it will return true or false.
$link = $a->href;
?>
Or:
<?php
$link = $html->find('a',0)->href;
?>
tag
- returns the tag nameinnertext
- returns inner HTML of an elementoutertext
- returns outer HTML of an elementplaintext
- returns plain text (without HTML tags)
Edit HTML Elements with PHP Simple HTML DOM Parser
<?php
// Change or set attribute value (if an attribute is a non-value attribute (e.g. checked, selected…), set its value to true or false).
$a->href = 'http://www.mydomain.com';
// Remove an attribute.
$a->href = null;
// Check if attribute exists
if(isset($a->href)) {
//do something here
}
?>
<?php
// Wrap an element
$e->outertext = '<div class="wrap">' . $e->outertext . '<div>';
// Remove an element
$e->outertext = '';
// Append an element
$e->outertext = $e->outertext . '<div>foo<div>';
// Insert an element
$e->outertext = '<div>foo<div>' . $e->outertext;
?>
To save the DOM document, just put the DOM object into a variable:
<?php
$doc = $html;
// Display the page
echo $doc;
?>
Avoid PHP Simple HTML DOM Parser Memory Leak
<?php
$html->clear();
?>
Author's Note
Comments: | ||
No Messages FoundYou can post questions/corrections/feedback here
| ||
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. |