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