Summary of simple_html_dom usage

  • 2020-07-21 07:00:48
  • OfStack


<P> Simple example 
<?phpinclude "simple_html_dom.php" ;&nbsp;&nbsp;&nbsp;&nbsp;// loading simple_html_dom.php file 
$html = file_get_html('http://www.google.com/');&nbsp;&nbsp;// To obtain html$dom = new simple_html_dom();&nbsp;&nbsp;&nbsp;&nbsp;//new simple_html_dom object $dom->load($html)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// loading html// Find all images foreach($dom->find('img') as $element) {&nbsp;&nbsp;&nbsp;// To obtain img Tag array &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $element->src . '<br>';&nbsp;&nbsp;&nbsp;&nbsp;// For each img In the tag src}// Find all links foreach($dom->find('a') as $element){ &nbsp;&nbsp;&nbsp;// To obtain a Array of labels &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $element->href . '<br>';&nbsp;&nbsp;&nbsp;&nbsp;// For each a In the tag href}</P><P>
$html = file_get_html('http://slashdot.org/');&nbsp;&nbsp;&nbsp;// To obtain html$dom = new simple_html_dom();&nbsp;&nbsp;&nbsp;&nbsp;//new simple_html_dom object $dom->load($html);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// loading html// Find all article blocksforeach($dom->find('div.article') as $article) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $item['title']&nbsp;&nbsp;&nbsp;&nbsp; = $article->find('div.title', 0)->plaintext;&nbsp;//plaintext  Get plain text &nbsp;&nbsp;&nbsp; $item['intro']&nbsp;&nbsp;&nbsp; = $article->find('div.intro', 0)->plaintext;&nbsp;&nbsp;&nbsp; $item['details'] = $article->find('div.details', 0)->plaintext;&nbsp;&nbsp;&nbsp; $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(); &nbsp;&nbsp;&nbsp;&nbsp;//new simple_html_dom object </P><P>$dom->load($html);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // loading html
$dom->find('div', 1)->class = 'bar';&nbsp;&nbsp;&nbsp;&nbsp;//class =  The assignment   For the first 2 a div the class The assignment </P><P>$dom->find('div[id=hello]', 0)->innertext = 'foo';&nbsp;&nbsp;&nbsp;//innertext Internal text </P><P>echo $dom; </P><P>// Output: <div id="hello">foo</div><div id="world" class="bar">World</div></P><P>&nbsp;</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 .  
&nbsp;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>&nbsp;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');&nbsp; </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');&nbsp; 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>&nbsp;5.Element&nbsp;  The method of 
$e = $html->find("div", 0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //$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>&nbsp;</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>


Related articles: