Tips for using PHP Regular Expressions

Source: Internet
Author: User
For beginners of php, we need to do some information capturing work. At the beginning, we used Simple-Html-Dom for webpage parsing. Although we found that it would be a better method to use it, however, the disadvantage is also fatal, slow, and inaccurate control. Later, Snoopy was introduced, and the effect was still not very good. Therefore, Curl + regular expressions were used for webpage crawling and parsing. Functi

For beginners of php, we need to do some information capturing work. At the beginning, we used Simple-Html-Dom for webpage parsing. Although we found that it would be a better method to use it, however, the disadvantage is also fatal, slow, and inaccurate control. Later, Snoopy was introduced, and the effect was still not very good. Therefore, Curl + regular expressions were used for webpage crawling and parsing. Functi

For beginners of php, we need to do some information capturing work. At the beginning, we used Simple-Html-Dom for webpage parsing. Although we found that it would be a better method to use it, however, the disadvantage is also fatal, slow, and inaccurate control. Later, Snoopy was introduced, and the effect was still not very good. Therefore, Curl + regular expressions were used for webpage crawling and parsing.

Function dangdang ($ keywords) {// $ keywords = "java"; // search keyword $ urls = 'HTTP: // search.dangdang.com /? Key = '. $ keywords. '& category_path = 01.00.00.00.00.00 & type = 01.00.00.00.00.00/'; // echo $ urls; $ st = curl_init (); curl_setopt ($ st, CURLOPT_URL, $ urls ); curl_setopt ($ st, CURLOPT_RETURNTRANSFER, 1); // The returned content is the string curl_setopt ($ st, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt ($ st, CURLOPT_MAXREDIRS, 2 ); // set curl_setopt ($ st, CURLOPT_TIMEOUT, 100) for a maximum of two redirection requests. // the waiting time cannot exceed five seconds $ dl_page = curl_exec ($ st ); $ info_arr = curl_getinfo ($ st); // var_dump ($ info_arr); preg_match_all ("/'; preg_match_all ("/\ btitle \ B = \" ([^ \ "] *) \" \ s * \ bclass \ B /", $ matches_all [0] [$ count], $ matches_title); echo ($ matches_title [1] [0]); echo'
'; Preg_match_all ("/\ bhttp \ B \: \/img. * \ bjpg \ B/", $ matches_all [0] [$ count], $ matches_img); echo (''); echo'
'; Preg_match_all ("/\ bhttp \ B ([^ A] *) \ bA \ B/", $ matches_all [0] [$ count], $ matches_url ); echo ($ matches_url [0] [0]); echo'
'; Preg_match_all ("/\ bhttp \ B \: \/\ bcomm \ B ([^ \"] *)/", $ matches_all [0] [$ count], $ matches_plurl); // preg_match_all ("/'; preg_match_all ("/\ bdetail \ B \" \ s * \> ([^ \ <] *) \ '; Preg_match_all ("/\ & \ byen \ B \; \ d + \. \ d/", $ matches_all [0] [$ count], $ matches_price); preg_match_all ("/\ d + \. \ d/", $ matches_price [0] [0], $ matches_price); echo ($ matches_price [0] [0]); // echo ($ matches_price [0] [0]); echo'
'; Preg_match_all ("/num \ B \" \> \ d */", $ matches_all [0] [$ count], $ matches_pinglun ); preg_match_all ("/\ d +/", $ matches_pinglun [0] [0], $ matches_pinglun); echo ($ matches_pinglun [0] [0]); echo'
'; Preg_match_all ("/\ btitle \ B = \' ([^ \ '] *) \'/", $ matches_all [0] [$ count], $ matches_price ); echo ($ matches_price [1] [0]); echo'
';}}

The code above captures and analyzes Dangdang's search data, retrieves the name of the book, the author's price profile, and other information. The following describes how to use regular expressions.

Take the Amazon website as an example:

View the source code on the search results page of the Amazon website and find the code of the search product results.

After seeing this source code, I believe that most people have a big head. We can carefully analyze the characteristics of the website page and find that the information of each product is saved in the li tag, in addition, each li has its own fixed ID, which is our breakthrough, that is, matching li and ID can determine the location of each product, note that the line breaks and empty lines are common in each li label. * fuzzy match cannot be performed. Therefore, line feed \ n and empty rows must be matched during fuzzy match.

preg_match_all("/\<\bli\b\s+\bid\b=\"result_[0-9]*.*([.\n]*)\\n.*\\n.*\\n[\\s\| ]*.*/",$dl_page,$matches_all);

In this way, you can determine the information of each product and then match the product name with the price. This reduces the number of incorrect matches.

?

?

Original article address: Tips for using PHP regular expressions. Thank you for sharing it with me.

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.