Вопрос

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

Это было полезно?

Решение

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

Другие советы

$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]);

Well, the problem here is, an address does not have to have a zip code with 4 digits. There are addresses with only 4 digits. Assuming that you have only 5 digit zip code addresses you could use a RegEx of course.

Have a look here, maybe this will help you:

Regex Expression to Find 5-Digit Code

If the last one is always the zip code and they all have 5 digit numbers, you can use something like this:

function getZipCode($address) {
    $ok = preg_match("/(\d\d\d\d\d)/", $address, $matches);
    if (!$ok) {
        // This address doesn't have a ZIP code
    }
    return $matches[count($matches] - 1];
}

I would look for all Numbers with 4 or 5 digits and take the last match.

preg_match( $addr, '/(\d{4,5})/', $matches);
$result = $matches[count($matches) - 1];

Well, this regex will return the last consecutive five digit string. It uses a negative look-ahead to ensure the absence of 5-digit strings after the one being returned

\b\d{5}\b(?!.*\b\d{5}\b)

so, perhaps:

if (preg_match('/\b\d{5}\b(?!.*\b\d{5}\b)/', $subject, $regs)) {
        $result = $regs[0];
} else {
    $result = "";
}
 var zipCode = vm.propertyAddress.match(/\d{5}(-\d{4})?\b/g);

Address : 8585 Summerdale rd Apt-175 SanDiego 92126 CA Result: 92126

This will also work for if only Zipcode is provided

Careful, parsing addresses is hard. A lot of these answers make precarious assumptions: mainly, that addresses are a regular language. They are not.

Unless your (US) addresses are guaranteed to be in a particular, standardized format (in which case, a regex might work, just for the ZIP code), you might want to try an API like LiveAddress (I work at SmartyStreets). APIs like this will parse the address for you, returning the components, and also verify it. (By the way, it appears that a few of the addresses you provided are invalid, as in, the USPS does not recognize them.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top