Parsing HTML tags using PHP simple HTML DOM Parser _php tutorial

Source: Internet
Author: User
Using PHP simple HTML DOM Parser parsing HTML tags using a PHP easy HTML DOM Parser parsing the HTML page, it feels good, it can create a DOM tree to facilitate you to parse the content inside the HTML. It's good to grab something. With an example, you also go to SourceForge to download the compressed package look inside the example: scraping data with PHP simple HTML DOM Parser php simple HTML DOM Parser, written in PHP 5+, allows-manipulate HTML in a very easy. Supporting invalid HTML, this parser are better then other PHP scripts using complicated regexes to extract information fro M web pages. Before getting the necessary info, a DOM should is created from either URL or file. The following 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 is 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 DOM from string$html = str_get_html (' Simpleparser '); $html->find (' div ', 1)->class = ' bar '; $html->find (' div[id=simple] ', 0)->innertext = ' Foo ';//Output:fooparserecho $html;D o wish to retrieve content without any tag S?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 thisParser ([Url]http://simplehtmldom.sourceforge.net/[/url]) You can find some scraping examples from Digg, IMDB, Slashdot. Let's create one that extracts the first results (titles only) for the keyword "php" from Google:view Plain copy to CLI Pboard 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 which has the class attribute equal with ' L ' foreach ($html->find (' a[class=l] ') as $key + = $inf O) {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 which has the class attribute equal with ' L ' foreach ($html->find (' a[class=l] ') as $key + = $inf O) {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 which has 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 a functions of It:view plain copy to clipboard print? PHP code include ' simple_html_dom.php '; [PHP] include ' simple_html_dom.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 to:

http://www.bkjia.com/PHPjc/477857.html www.bkjia.com true http://www.bkjia.com/PHPjc/477857.html techarticle using PHP simple HTML DOM Parser parsing HTML tags with a bit of PHP easy HTML DOM Parser parsing HTML page, it feels good, it can create a DOM tree for you to parse inside the HTML ...

  • Related Article

    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.