src/sb/Validate/ContactInfo.php
public static function zip($zip, $check_usps = true) {

        $result = new \sb\Validate\Results();
        $result->value = $zip;
        $result->is_valid = false;

        if (preg_match("/^(\d{5})(-\d{4})*$/", $zip)) {
            $result->message = "Valid zip code format";
            $result->is_valid = true;

            if ($check_usps) {

                $page = @file_get_contents("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=2&postalCode=" . substr($zip, 0, 5));

                if (!$page) {
                    $result->message .= ' cannot reach USPS site to validate zip code existence';
                } else {
                    preg_match("~<p class=\"std-address\">(.*?)</p>~", $page, $city);

                    if (isset($city[1])) {
                        $data = trim($city[1]);
                        $result->state = substr($data, -2, 2);
                        $result->city = ucwords(strtolower(preg_replace("~" . $result->state . "$~", "", $data)));
                        $result->message .= " for " . $result->city . ',' . $result->state;
                    } else {
                        $result->message .= " but city not found!";
                        $result->is_valid = false;
                    }
                }
            }
        } else {
            $result->message = "Invalid zip code format ";
        }


        return $result;
    }
public static function zip($zip, $check_usps = true) {

        $result = new \sb\Validate\Results();
        $result->value = $zip;
        $result->is_valid = false;

        if (preg_match("/^(\d{5})(-\d{4})*$/", $zip)) {
            $result->message = "Valid zip code format";
            $result->is_valid = true;

            if ($check_usps) {

                $page = @file_get_contents("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=2&postalCode=" . substr($zip, 0, 5));

                if (!$page) {
                    $result->message .= ' cannot reach USPS site to validate zip code existence';
                } else {
                    preg_match("~<p class=\"std-address\">(.*?)</p>~", $page, $city);

                    if (isset($city[1])) {
                        $data = trim($city[1]);
                        $result->state = substr($data, -2, 2);
                        $result->city = ucwords(strtolower(preg_replace("~" . $result->state . "$~", "", $data)));
                        $result->message .= " for " . $result->city . ',' . $result->state;
                    } else {
                        $result->message .= " but city not found!";
                        $result->is_valid = false;
                    }
                }
            }
        } else {
            $result->message = "Invalid zip code format ";
        }


        return $result;
    }
public static function zip ( $ zip , $ check_usps = true ) { $ result = new \ sb \ Validate \ Results ( ) ; $ result -> value = $ zip ; $ result -> is_valid = false ; if ( preg_match ( "/^(\d{5})(-\d{4})*$/" , $ zip ) ) { $ result -> message = "Valid zip code format" ; $ result -> is_valid = true ; if ( $ check_usps ) { $ page = @ file_get_contents ( "https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=2&postalCode=" . substr ( $ zip , 0 , 5 ) ) ; if ( ! $ page ) { $ result -> message .= ' cannot reach USPS site to validate zip code existence' ; } else { preg_match ( "~<p class=\"std-address\">(.*?)</p>~" , $ page , $ city ) ; if ( isset ( $ city [ 1 ] ) ) { $ data = trim ( $ city [ 1 ] ) ; $ result -> state = substr ( $ data , - 2 , 2 ) ; $ result -> city = ucwords ( strtolower ( preg_replace ( "~" . $ result -> state . "$~" , "" , $ data ) ) ) ; $ result -> message .= " for " . $ result -> city . ',' . $ result -> state ; } else { $ result -> message .= " but city not found!" ; $ result -> is_valid = false ; } } } } else { $ result -> message = "Invalid zip code format " ; } return $ result ; }
Validates a zip code

@author paul.visco@roswellpark.org
@version 1.2 13/18/2008
@param string $zip The zip code to validate in xxxxx or xxxxx-xxxx format
@param boolean check_usps Check the usps sie look for validation

@return \sb\Validate_Results The message property includes the city if it exists
Validates a zip code