Summary of simple_html_dom usage
- 2020-07-21 07:00:48
- OfStack
<P> Simple example
<?phpinclude "simple_html_dom.php" ; // loading simple_html_dom.php file
$html = file_get_html('http://www.google.com/'); // To obtain html$dom = new simple_html_dom(); //new simple_html_dom object $dom->load($html) // loading html// Find all images foreach($dom->find('img') as $element) { // To obtain img Tag array echo $element->src . '<br>'; // For each img In the tag src}// Find all links foreach($dom->find('a') as $element){ // To obtain a Array of labels echo $element->href . '<br>'; // For each a In the tag href}</P><P>
$html = file_get_html('http://slashdot.org/'); // To obtain html$dom = new simple_html_dom(); //new simple_html_dom object $dom->load($html); // loading html// Find all article blocksforeach($dom->find('div.article') as $article) { $item['title'] = $article->find('div.title', 0)->plaintext; //plaintext Get plain text $item['intro'] = $article->find('div.intro', 0)->plaintext; $item['details'] = $article->find('div.details', 0)->plaintext; $articles[] = $item;}print_r($articles);</P><P>}</P><P>
// Create DOM from string</P><P>$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$dom = new simple_html_dom(); //new simple_html_dom object </P><P>$dom->load($html); // loading html
$dom->find('div', 1)->class = 'bar'; //class = The assignment For the first 2 a div the class The assignment </P><P>$dom->find('div[id=hello]', 0)->innertext = 'foo'; //innertext Internal text </P><P>echo $dom; </P><P>// Output: <div id="hello">foo</div><div id="world" class="bar">World</div></P><P> </P><P>DOM methods & properties
Name Description
void __construct ( [string $filename] ) The constructor , The filename parameter will automatically load the content , Either text or file / url .
string plaintext Plain text
void clear () Clean up the memory
void load ( string $content ) Loading content
string save ( [string $filename] ) Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file.
void load_file ( string $filename ) Load contents from a from a file or a URL.
void set_callback ( string $function_name ) Set up the 1 A callback function.
mixed find ( string $selector [, int $index] ) Element-Finding CSS Selector. Returns the first n Element object if index set , Otherwise returns 1 An array object. </P>
<P> 4.find Detailed description of methods </P><P>
find ( string $selector [, int $index] )
// Find all anchors, returns a array of element objects a Tag array
$ret = $html->find('a');</P><P>// Find (N)th anchor, returns element object or null if not found (zero based) The first 1 a a The label
$ret = $html->find('a', 0);</P><P>// Find lastest anchor, returns element object or null if not found (zero based) The last 1 a a The label
$ret = $html->find('a', -1); </P><P>// Find all <div> with the id attribute
$ret = $html->find('div[id]');</P><P>// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]'); </P><P>
// Find all element which id=foo
$ret = $html->find('#foo');</P><P>// Find all element which class=foo
$ret = $html->find('.foo');</P><P>// Find all element has attribute id
$ret = $html->find('*[id]'); </P><P>// Find all anchors and images a Labels and img Tag array
$ret = $html->find('a, img'); </P><P>// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');</P><P>
// Find all <li> in <ul>
$es = $html->find('ul li'); ul Under the label of li Tag array </P><P>// Find Nested <div> tags
$es = $html->find('div div div'); div Under the label div Under the label div Tag array </P><P>// Find all <td> in <table> which class=hello
$es = $html->find('table.hello td'); table Under the label td Tag array </P><P>// Find all td tags with attribite align=center in table tags
$es = $html->find(''table td[align=center]'); </P><P> 5.Element The method of
$e = $html->find("div", 0); //$e The methods that are owned are shown in the following table
Attribute Name Usage
$e->tag The label
$e->outertext Outside the text
$e->innertext The text within
$e->plaintext Plain text </P><P> </P><P>// Example
$html = str_get_html("<div>foo <b>bar</b></div>");
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
echo $e->innertext; // Returns: " foo <b>bar</b>"
echo $e->plaintext; // Returns: " foo bar"</P><P>6.DOM traversing methods
Method Description
mixed$e->children ( [int $index] ) Child elements
element$e->parent () The parent element
element$e->first_child () The first 1 child
element$e->last_child () The last 1 child
element$e->next_sibling () after 1 Sibling element
element$e->prev_sibling () before 1 Sibling element </P><P>
// 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');
</P>