开发者

How do I read a postal code from a string?

开发者 https://www.devze.com 2023-03-25 20:34 出处:网络
I want to read a postal code from a string. The algorithm should see the XXXXX numbers, and also 5 numbers cons开发者_如何学编程ecutively - like 12345, 68376, 78902, etc.

I want to read a postal code from a string. The algorithm should see the XXXXX numbers, and also 5 numbers cons开发者_如何学编程ecutively - like 12345, 68376, 78902, etc.

How can I do this with PHP?


preg_match('/[0-9]{5}/', $str, $zip);

echo $zip[0];


preg_match('/[^0-9][0-9]{5}[^0-9]/', $str, $zip);

i modified PMV's answer so that it will not match digit strings longer then 5.


$num=12345;
if(is_numeric($num) and strlen($num)==5){ do it }


I recently had to extract parts of an address from a single string for a real estate website, and I found that the best option was to use google geocode API. It allowed me to get Street, City, State, Zip, Latitude, Longitude, and more for every address entered.

I found a great guide on getting set up with google geocode API (PHP) here: http://www.andrew-kirkpatrick.com/2011/10/google-geocoding-api-with-php/

The best part, it even works with names of places. So a search for 'UCLA' will give you all the parts of an address that you might need.

0

精彩评论

暂无评论...
验证码 换一张
取 消