API Reference
Helper functions
Object Str_get_html (String $content) creates a DOM object from a string.
Object File_get_html (String $filename) creates a DOM object from a file or a URL.
DOM Methods & Properties
Stringplaintext Returns the contents extracted from HTML.
Voidclear () Clean up memory.
Voidload (String $content) Load contents from a string.
Stringsave ([string $filename]) dumps the internal DOM tree back into a string. If the $filename is set, result string would save to file.
Voidload_file (string $filename) Load contents from a from a file or a URL.
Voidset_callback (String $function _name) Set a callback function.
Mixedfind (String $selector [, int $index]) Find elements by the CSS selector. Returns the Nth Element object if index is set, otherwise return an array of object.
Element Methods & Properties
String[attribute] Read or write element ' s attribure value.
Stringtag Read or write the tag name of element.
Stringoutertext Read or write the outer HTML text of element.
Stringinnertext Read or write the inner HTML text of element.
Stringplaintext Read or write the plain text of element.
Mixedfind (String $selector [, int $index]) Find children by the CSS selector. Returns the Nth Element object if index is set, otherwise, and return an array of object.
DOM traversing
Mixed$e->children ([int $index]) Returns The Nth child object if index is set, otherwise return an array of children.
Element$e->parent () Returns the parent of element.
Element$e->first_child () Returns The first child of element, or null if not found.
Element$e->last_child () Returns The last child of element, or null if not found.
Element$e->next_sibling () Returns the next sibling of element, or null if not found.
Element$e->prev_sibling () Returns the previous sibling of element, or null if not found.
Camel naming convertions You can also call methods with the standard camel naming convertions.
String$e->getattribute ($name) String$e->attribute
Void$e->setattribute ($name, $value) Void$value = $e->attribute
Bool$e->hasattribute ($name) boolisset ($e->attribute)
Void$e->removeattribute ($name) Void$e->attribute = null
Element$e->getelementbyid ($id) Mixed$e->find ("# $id", 0)
Mixed$e->getelementsbyid ($id [, $index]) mixed$e->find ("# $id" [, int $index])
Element$e->getelementbytagname ($name) mixed$e->find ($name, 0)
Mixed$e->getelementsbytagname ($name [, $index]) mixed$e->find ($name [, int $index])
Element$e->parentnode () element$e->parent ()
Mixed$e->childnodes ([$index]) mixed$e->children ([int $index])
Element$e->firstchild () Element$e->first_child ()
Element$e->lastchild () Element$e->last_child ()
Element$e->nextsibling () element$e->next_sibling ()
Element$e->previoussibling () element$e->prev_sibling ()
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 ');
Create a DOM object
$html = new Simple_html_dom ();
Load HTML from a string
$html->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 all anchors, returns a array of element objects
$ret = $html->find (' a ');
Find (N) Thanchor, 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] ');
Find all element which Id=foo
$ret = $html->find (' #foo ');
Find all element which Class=foo
$ret = $html->find ('. Foo ');
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
Inch
$es = $html->find (' ul Li ');
Find Nested Tags
$es = $html->find (' div div div ');
Find AllInch
- Inch
foreach ($html->find (' ul ') as $ul)
{
foreach ($ul->find (' Li ') as $li)
{
Do something ...
}
}
Find First
- In first
$e = $html->find (' ul ', 0)->find (' Li ', 0);
Supports These operators in attribute selectors:
[Attribute] Matches elements that has the specified attribute.
[Attribute=value] Matches elements that has the specified attribute with a certain value.
[Attribute!=value] Matches elements that don ' t has the specified attribute with a certain value.
[Attribute^=value] Matches elements that has the specified attribute and it starts with a certain value.
[Attribute$=value] Matches elements that has the specified attribute and it ends with a certain value.
[Attribute*=value] Matches elements that has the specified attribute and it contains a certain value.
Find All text blocks
$es = $html->find (' text ');
Find All Comment ( ) blocks
$es = $html->find (' comment ');
Get a attribute (If the attribute is Non-value attribute (eg checked, selected ...), it'll 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;
Determine whether a attribute exist?
if (Isset ($e->href))
echo ' href exist! ';
Example
$html = str_get_html ("foo
Bar");
$e = $html->find ("div", 0);
Echo $e->tag; Returns: "Div"
Echo $e->outertext; Returns: "foo
Bar"
Echo $e->innertext; Returns: "foo
Bar"
Echo $e->plaintext; Returns: "Foo bar"
$e->tag Read or write the tag name of element.
$e->outertext Read or write the outer HTML text of element.
$e->innertext Read or write the inner HTML text of element.
$e->plaintext Read or write the plain text of element.
Extract contents from HTML
Echo $html->plaintext;
Wrap a element
$e->outertext = '. $e->outertext. '';
Remove a element, set it ' s outertext as an empty string
$e->outertext = ";
Append a element
$e->outertext = $e->outertext. ' Foo ';
Insert a element
$e->outertext = ' foo '. $e->outertext;
If you aren't so familiar with the HTML DOM, check this link to learn more ...
Example
echo $html->find ("#div1", 0)->children (1)->children (1)->children (2)->id;
Or
echo $html->getelementbyid ("Div1")->childnodes (1)->childnodes (1)->childnodes (2)->getattribute (' Id ');
You can also call methods with Camel naming convertions.
Mixed$e->children ([int $index]) Returns The Nth child object if index is set, otherwise return an array of children.
Element$e->parent () Returns the parent of element.
Element$e->first_child () Returns The first child of element, or null if not found.
Element$e->last_child () Returns The last child of element, or null if not found.
Element$e->next_sibling () Returns the next sibling of element, or null if not found.
Element$e->prev_sibling () Returns the previous sibling of element, or null if not found.
Dumps the internal DOM tree back into string
$str = $html;
Print it!
Echo $html;
Dumps the internal DOM tree back into string
$str = $html->save ();
Dumps the internal DOM tree back into a file
$html->save (' result.htm ');
Write a function with parameter "$element"
function My_callback ($element) {
Hide All
Tags
if ($element->tag== ' B ')
$element->outertext = ";
}
Register the callback function with it ' s function name
$html->set_callback (' My_callback ');
Callback function would be invoked while dumping
Echo $html;The above describes the Tooyoungtoosimple simplehtmldom Doc API help documentation, including the tooyoungtoosimple aspect of the content, I hope to be interested in PHP tutorial friends helpful.
-
which Class=hello $es = $html->find (' Table.hello TD '); Find all TD tags with attribite align=center in table tags $es = $html->find (' table td[align=center] '); Find All