Php crawls Baidu zip code search results. How should I write regular expressions?

Source: Internet
Author: User
Tags phpfiddle
I am a little white. I want to use Baidu zip code to search for an address's zip code and put a php script in my website space. Because the requirements are not very high, I tried to use regular expression matching to find the first group of "Six digits not 0 at the first place" as the result output. However, no matter how you try it, the result is always... I am a little white. I want to use Baidu zip code to search for an address's zip code and put a php script in my website space.
Because the requirement is not very high, I tried to use regular expression matching to find the first group of "Six digits not 0 at the first place" as the result output. However, no matter how I try it, the result is always an empty array ......
Regular Expressions are obtained from the Internet. They are also in different forms, but they never get any value.


  ");echo($clear_result);echo("
");$zipcodes = search_for_zipcodes($clear_result);print_r($zipcodes);function search_for_zipcodes($test) { $rule = '/^[1-9]\d{5}$/'; preg_match_all($rule,$test,$result); return $result;} ?>

mb_detect_encoding($search_result)To see the code of the returned page, but this function does not seem to have any results ......

If you are confused, please be patient. I am a little white ......

Reply content:

I am a little white. I want to use Baidu zip code to search for an address's zip code and put a php script in my website space.
Because the requirement is not very high, I tried to use regular expression matching to find the first group of "Six digits not 0 at the first place" as the result output. However, no matter how I try it, the result is always an empty array ......
Regular Expressions are obtained from the Internet. They are also in different forms, but they never get any value.


  ");echo($clear_result);echo("
");$zipcodes = search_for_zipcodes($clear_result);print_r($zipcodes);function search_for_zipcodes($test) { $rule = '/^[1-9]\d{5}$/'; preg_match_all($rule,$test,$result); return $result;} ?>

mb_detect_encoding($search_result)To see the code of the returned page, but this function does not seem to have any results ......

If you are confused, please be patient. I am a little white ......

Hello.

I don't think you can use regular expressions for this task.

Why?

What are the search results?

        
  
  
Zip code Administrative Region
250102 Shandong Jinan City Historical Zone2218-5150
250102 Shandong Jinan City Historical ZoneNo. 33188-33688, No. 2, east longitude 10 road
250102 Shandong Jinan City Historical ZoneJing Shi Road No. 177-3799
250102 Shandong Jinan City Historical ZoneGanggou water conservancy station apartment, tourism Road

The complete result is a list.

Because I haven't looked at it yet. I'll tell you how to handle it later. I'll give it a try in php first.

  
  LoadHTMLFile ($ url); libxml_clear_errors (); $ xPath = new DOMXPath ($ dom ); // statement $ trs = $ xPath-> query ('// li/a/text () | // tr/td '); // output result foreach ($ trs as $ tr) {$ str = $ tr-> nodeValue; if (strlen ($ str)> 6) {$ str = array_pop (explode ('', $ str) ;}echo $ str; break ;}?>

Http://phpfiddle.org/

You can try phpfiddle. It's okay. To do this kind of work, xpath is more convenient.


  (\d+)(.*?)@is", $search_result, $match);$area = array_map("strip_tags", $match[2]);print_r($match[1]);print_r($area);

Replace $ rule = '/^ [1-9] \ d {5} $/'
$ Rule = '/[1-9] \ d {5}/'; try
Because you have used strip_tags. Therefore, many contents are directly merged into "one row. Use ^ again, $ should not be matched

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.