PHP uses Simple_html_dom to parse HTML samples _php tips

Source: Internet
Author: User
Tags php programming php regular expression

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.