Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a zip code from an address string

Tags:

regex

php

zipcode

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..

like image 216
I wrestled a bear once. Avatar asked Jan 12 '14 12:01

I wrestled a bear once.


People also ask

How do you extract ZIP codes from a string in Excel?

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).

How to extract postcode from full address in Excel?

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.

How many numbers are there in a ZIP code?

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.

How do I find the ZIP code of a given 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:


2 Answers

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

Using it in your PHP:

$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

EDIT 1:

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

like image 179
Ilia Avatar answered Oct 19 '22 20:10

Ilia


$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]);
like image 1
Ðimple Vasoya Avatar answered Oct 19 '22 18:10

Ðimple Vasoya