This example describes how PHP uses Simple_html_dom to parse HTML. Share to everyone for your reference, specific as follows:
Today, I wrote two reptiles, one using Python, one using PHP, and to be honest, both implementations are convenient.
Here's an open source class that uses Simple_html_dom to parse the HTML code:
1. Download Simple_html_dom:
http://sourceforge.net/projects/simplehtmldom/files/
or click here to download the site.
2. Usage examples:
<?php//example of how to use basic selector to retrieve HTML contents include ('.
/simple_html_dom.php ');
Get DOM from URL or file $html = file_get_html (' http://www.google.com/'); Find all link foreach ($html->find (' a ') as $e) echo $e->href.
' <br> '; Find all image foreach ($html->find (' img ') as $e) echo $e->src.
' <br> '; Find all image with the full tag foreach ($html->find (' img ') as $e) echo $e->outertext.
' <br> '; Find all div tags with id=gbar foreach ($html->find (' Div#gbar ') as $e) echo $e->innertext.
' <br> '; Find all spans tags with class=gb1 foreach ($html->find (' span.gb1 ') as $e) echo $e->outertext.
' <br> '; Find all TD tags with attribite align=center foreach ($html->find (' td[align=center]) as $e) echo $e->innertext .
' <br> '; Extract text from table echo $html->find (' td[align= "center"] ', 1)->plaintext. '
<br>
For more information about PHP interested readers can view the site topics: "PHP coding and transcoding Operation tips Summary", "PHP object-oriented Programming Introduction Course", "PHP Mathematical Calculation Skills Summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.