_php Tutorial for parsing HTML implementation code in PHP

Source: Internet
Author: User
Recently want to write a crawler in PHP, you need to parse the HTML, found on SourceForge a project called PHP simple HTML DOM Parser, it can be similar to jquery through the CSS selector to return the specified DOM elements, the function is very powerful.
The first thing to do is introduce simple_html_dom.php this file at the beginning of the program
Copy CodeThe code is as follows:
Include_once (' simple_html_dom.php ');

PHP Simple HTML DOM Parser provides 3 ways to create DOM objects
Copy CodeThe code is as follows:
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 ');

You can do all kinds of things after you get the DOM object.
Copy CodeThe code is as follows:
Find all anchors, returns a array of element objects
$ret = $html->find (' a ');
Find (N) th anchor, returns element object or null if not found (zero based)
$ret = $html->find (' A ', 0);
Find Lastest Anchor, returns element object or null if not found (zero based)
$ret = $html->find (' A ',-1);
Find all with the id attribute
$ret = $html->find (' div[id] ');
Find all which attribute Id=foo
$ret = $html->find (' div[id=foo] ');

A variety of CSS selectors can be used here, just as handy for DOM operations in jquery. In addition, there are two special attributes to get the contents of text and annotations
Copy CodeThe code is as follows:
Find All text blocks
$es = $html->find (' text ');
Find All Comment ( ) blocks
$es = $html->find (' comment ');

Of course, similar to jquery,php simple HTML DOM parser also supports chained operations, as well as a variety of easy ways to access DOM elements
Copy CodeThe code is as follows:
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 ');

http://www.bkjia.com/PHPjc/324200.html www.bkjia.com true http://www.bkjia.com/PHPjc/324200.html techarticle recently want to write a crawler in PHP, you need to parse the HTML, found on SourceForge a project called PHP simple HTML DOM Parser, it can be similar to jquery in the way of the CSS selector to return ...

  • 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.