I have some full addresses, for example:
$addr1 = "5285 KEYES DR KALAMAZOO MI 49004 2613"
$addr2 = "PO BOX 35 COLFAX LA 71417 35"
$addr3 = "64938 MAGNOLIA LN APT B PINEVILLE LA 71360-9781"
I need to get the 5-digit zip code out of the string. How can I do that? Perhaps with RegEx?
An acceptable answer assumes that there could be multiple 5-digit numbers in an address, but the Zip code will always be the last consecutive 5 digit number.
My idea was to use explode then loop through and check each index. Anyone got a better idea?
Any help is greatly appreciated..
To extract the codes, we start by entering the RIGHT formula in the cell where we would like our zip codes to appear. Like it's opposite number, LEFT, the RIGHT formula directs Excel to count the number of characters in a text string from the right (or left, with LEFT).
Extract postcode with VBA in Excel There is a VBA that can extract postcodes from full addresses quickly in Excel. 1. Select a cell of the column you want to select and press Alt + F11 to open the Microsoft Visual Basic for Applicationswindow. 2. In the pop-up window, click Insert> Module, then paste the following VBA code into the module.
Note: As the above notes mentioned, the number 10 in the formula is the tenth character after the first comma; and the number 5 means that there are five numbers constitute the zip code. Please click How to extract postcode from address list in Excel? for more details of extract zip code from address.
We select the cell, A2, then count the number of characters used in the zip code, in this case 5. Our formula should now read =RIGHT (A2,5) Hit return and we will see the zip code appear. Copy all the way down to get the zip codes for the top five addresses:
Speaking about US zip-codes, which are pre-followed with two letter state code in order to get a zip-code you could use the following regex:
/\b[A-Z]{2}\s+\d{5}(-\d{4})?\b/
Explanation:
\b # word boundary
[A-Z]{2} # two letter state code
\s+ # whitespace
\d{5} # five digit zip
(-\d{4})? # optional zip extension
\b # word boundary
Online Example
$addr1 = "5285 KEYES DR KALAMAZOO MI 49004 2613";
$addr2 = "PO BOX 35 COLFAX LA 71417 35";
$addr3 = "64938 MAGNOLIA LN APT B PINEVILLE LA 71360-9781";
function extract_zipcode($address) {
$zipcode = preg_match("/\b[A-Z]{2}\s+\d{5}(-\d{4})?\b/", $address, $matches);
return $matches[0];
}
echo extract_zipcode($addr1); // MI 49004
echo extract_zipcode($addr2); // LA 71417
echo extract_zipcode($addr3); // LA 71360-9781
Online Example
In order to extend functionality and flexibility, you can specify if you wish to keep state code or not:
function extract_zipcode($address, $remove_statecode = false) {
$zipcode = preg_match("/\b[A-Z]{2}\s+\d{5}(-\d{4})?\b/", $address, $matches);
return $remove_statecode ? preg_replace("/[^\d\-]/", "", extract_zipcode($matches[0])) : $matches[0];
}
echo extract_zipcode($addr1, 1); // 49004 (without state code)
echo extract_zipcode($addr2); // LA 71417 (with state code)
Online Example
$addr = "U Square, The Park, On NH-39, Village- Kupa, Taluka- Bhiwandi, District Thane 421101, test test, 454564";
$zipcode = preg_match("/\b\d{6}\b/", $a, $matches); //It will return first occurance of 6 digit no. i.e. Indian pincode
print_r($matches[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With