Use phpsimplehtmldomparser to parse html tags

Source: Internet
Author: User
Phpsimplehtmldomparser is used to parse html tags and PHPSimpleHTMLDOMParser is used to parse HTML pages. it feels pretty good. it can create a DOMtree so that you can easily parse the content in html. It's good to catch things. With an example, you also go to s... syntaxH uses php simple html dom parser to parse html tags and uses PHP Simple html dom Parser to parse HTML pages. it feels pretty good. it can create a DOM tree for you to easily parse the content in html. It's good to catch things. Here is an example: Scraping data with PHP Simple html dom Parser, written in PHP5 +, allows you to manipulate HTML in a very easy way. supporting invalid HTML, this parser is better then other PHP scripts using complicated regexes to extract information from web pages. before getting the necessary info, a DOM shocould be created from either URL or file. the fo Lowing script extracts links & images from a website: view plain copy to clipboard print? Php code // Create DOM from URL or file $ html = file_get_html (' http://www.microsoft.com/ '); // Extract links foreach ($ html-> find ('A') as $ element) echo $ element-> href .'
'; // Extract images foreach ($ html-> find ('IMG') as $ element) echo $ element-> src .'
'; [Php] // Create DOM from URL or file $ html = file_get_html ('http: // www.microsoft.com /'); // Extract links foreach ($ html-> find ('A') as $ element) echo $ element-> href.'
'; // Extract images foreach ($ html-> find ('IMG') as $ element) echo $ element-> src .'
'; // Create DOM from URL or file $ html = file_get_html ('http: // www.microsoft.com/'); // Extract linksforeach ($ html-> find ('A ') as $ element) echo $ element-> href.'
'; // Extract imagesforeach ($ html-> find ('IMG') as $ element) echo $ element-> src .'
'; The parser can also be used to modify HTML elements: view plain copy to clipboard print? Php code // Create DOM from string $ html = str_get_html ('simpleparser '); $ html-> find ('div', 1)-> class = 'bar '; $ html-> find ('div [id = simple] ', 0)-> innertext = 'foo'; // Output: FooParser echo $ html; [php] // Create DOM from string $ html = str_get_html ('simpleparser '); $ html-> find ('div', 1)-> class = 'bar '; $ html-> find ('div [id = simple] ', 0)-> innertext = 'foo'; // Output: FooParser echo $ html; // Create DO M from string $ html = str_get_html ('simpleparser '); $ html-> find ('div', 1)-> class = 'bar '; $ html-> find ('div [id = simple] ', 0)-> innertext = 'foo'; // Output: FooParserecho $ html; do you wish to retrieve content without any tags? View plain copy to clipboard print? Php code echo file_get_html (' http://www.yahoo.com/ ')-> Plaintext; [php] echo file_get_html (' http://www.yahoo.com/ ')-> Plaintext; echo file_get_html (' http://www.yahoo.com/ ')-> Plaintext; In the package files of this parser ([url] http://simplehtmldom.sourceforge.net/ [/Url]) you can find some scraping examples from digg, imdb, slashdot. let's create one that extracts the first 10 results (titles only) for the keyword "php" from Google: view plain copy to clipboard print? Php code $ url =' http://www.google.com/search?hl=en&q=php&btnG=Search '; // Create DOM from URL $ html = file_get_html ($ url ); // Match all 'A' tags that have the class attribute equal with 'L' foreach ($ html-> find ('a [class = l] ') as $ key => $ info) {echo ($ key + 1 ). '. '. $ info-> plaintext."
\ N ";} [php] $ url = 'http: // www.google.com/search? Hl = en & q = php & btnG = search'; // Create DOM from URL $ html = file_get_html ($ url ); // Match all 'A' tags that have the class attribute equal with 'L' foreach ($ html-> find ('a [class = l] ') as $ key => $ info) {echo ($ key + 1 ). '. '. $ info-> plaintext."
\ N ";}$ url = 'http: // www.google.com/search? Hl = en & q = php & btnG = search'; // Create DOM from URL $ html = file_get_html ($ url ); // Match all 'A' tags that have the class attribute equal with 'L' foreach ($ html-> find ('a [class = l] ') as $ key => $ info) {echo ($ key + 1 ). '. '. $ info-> plaintext."
\ N ";} NOTE Make sure to include the parser before using any functions of it: view plain copy to clipboard print? Php code include 'simple _ html_dom.php '; [php] include 'simple _ html_dom.php '; for more information regarding the usage of this function consider checking the 'php Simple HTML Dom Parser 'Manual. to download the package files use the following URL: [url] Share:

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.