在PHP中从String中提取City和Zipcode

时间:2014-12-17 22:11:44

标签: php preg-match match

我需要在PHP中使用快速通用方法从输入字符串中提取City和Zipcode(如果可用)信息。

字符串可以是以下形式

  1. $ input_str =“123 Main Street,New Haven,CT”;
  2. $ input_str =“123 Main Street,New Haven,CT 06510”;
  3. $ input_str =“New Haven,CT,United States”;
  4. $ input_str =“New Haven,CT 06510”;
  5. 我在想(1)& (3)至少我可以用“,”来爆炸输入字符串,然后通过数组循环找到一个2位数的STATE字符并忽略它。但是我超越了这一点。

    $search_values = explode(',' ,$input_str);
    foreach($search_values as $search)
    {
        $trim_search = trim($search);   // Remove any trailing white spaces
    
        // If the 2 digit State is provided without Zipcode, ignore it
        if (strlen($trim_search) == 2)
        {
            //echo 'Ignoring State Without Zipcode: ' . $search . '<br>';
            continue;
        }
    
        ...
    

1 个答案:

答案 0 :(得分:1)

我不是最强大的正则表达式,但是这里有一个找到带有或不带有邮政编码的2字符状态的镜头。

正则表达式:(([A-Z]{2})|[0-9]{5})+

Fiddle

但是,如果您只想在状态和邮政编码在一起时匹配,请看一下: 正则表达式:(([A-Z]{2})(\s*[0-9]{5}))+

Fiddle

希望这有点帮助。

修改

class Extract  {

    private $_string;

    private $_sections = array();

    private $_output = array();

    private $_found = array();

    private $_original_string;

    private $_countries = array (
        'United States',
        'Canada',
        'Mexico',
        'France',
        'Belgium',
        'United Kingdom',
        'Sweden',
        'Denmark',
        'Spain',
        'Australia',
        'Austria',
        'Italy',
        'Netherlands'
    );

    private $_zipcon = array();

    private $ZIPREG = array(
        "United States"=>"^\d{5}([\-]?\d{4})?$",
        "United Kingdom"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
        "Germany"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
        "Canada"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\s*(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
        "France"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
        "Italy"=>"^(V-|I-)?[0-9]{5}$",
        "Australia"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
        "Netherlands"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
        "Spain"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
        "Denmark"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
        "Sweden"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
        "Belgium"=>"^[1-9]{1}[0-9]{3}$"
    ); // thanks to http://www.pixelenvision.com/1708/zip-postal-code-validation-regex-php-code-for-12-countries/

    public function __construct($string) {

        $this->_output = array (

            "state" => "",
            "city" => "",
            "country" => "",
            "zip" => "",
            "street" =>"",
            "number" => ""
        );
        $this->_original_string = $string;
        $this->_string = $this->normalize(trim($string));


        // create an array of patterns in order to extract zip code using the country list we already have
        foreach($this->ZIPREG as $country => $pattern) {
            $this->_zipcon[] = $pattern = preg_replace( array("/\^/","/\\$/"),array("",""), $pattern);
        }

        $this->init();

    }

    protected function init() {

        $this->getData(); // get data that can be found without breaking up the string.

        $this->_sections = array_filter(explode(',', trim($this->_string)));  // split each section

        if(!empty($this->_sections)) {
            foreach($this->_sections as $i => $d) {
                $d = preg_replace(array("/\s+/", "/\s([?.!])/"),  array(" ","$1"), $d ); 
                $this->_sections[$i] = trim($this->normalize($d));  // normalize strin to have one spacing between each word
            }
        } else {
            $this->_sections[] = $this->_string;    
        }       

        // try to match what's missing with has already been found
        $notFound = $this->getNotFound();
        if(count($notFound)==1 && count($this->_found)>1) {
            $found = $this->getFound();
            foreach($found as $string) {
                $notFound[0] = preg_replace("/$string/i", "", $notFound[0]);
            }
            $this->_output["city"] = $notFound[0];
            $this->_found[] = $this->_output["city"];
            $this->remove($this->_output["city"]);
        }   
    }

    public function getSections() {
        return $this->_sections;
    }   

    protected function normalize($string) {
        $string = preg_replace(array("/\s+/", "/\s([?.!])/"),  array(" ","$1"), trim($string));
        return $string;
    }

    protected function country_from_zip($zip) {
        $found = "";
        foreach($this->ZIPREG as $country => $pattern) {
            if(preg_match ("/".$pattern."/", $zip)) {
                $found = $country;
                break;
            }
        }
        return $found;
    }

    protected function getData() {
        $container = array();
        // extract zip code only when present beside state, or else five digits are meaningless

        if(preg_match ("/[A-Z]{2,}\s*(".implode('|', $this->_zipcon).")/", $this->_string) ){
            preg_match ("/[A-Z]{2,}\s*(".implode('|', $this->_zipcon).")/", $this->_string, $container["state_zip"]);

            $this->_output["state"] = $container["state_zip"][0];
            $this->_output["zip"] = $container["state_zip"][1];
            $this->_found[] = $this->_output["state"] . " ". $this->_output["zip"];
            // remove from string once found
            $this->remove($this->_output["zip"]);   
            $this->remove($this->_output["state"]);

            // check to see if we can find the country just by inputting zip code
            if($this->_output["zip"]!="" ) {
                $country = $this->country_from_zip($this->_output["zip"]);
                $this->_output["country"] = $country;
                $this->_found[] = $this->_output["country"];
                $this->remove($this->_output["country"]);
            }
        } 

        if(preg_match ("/\b([A-Z]{2,})\b/", $this->_string)) {
            preg_match ("/\b([A-Z]{2,})\b/", $this->_string, $container["state"]);  
            $this->_output["state"] = $container["state"][0];
            $this->_found[] = $this->_output['state'];
            $this->remove($this->_output["state"]);
        }

        // if we weren't able to find a country based on the zip code, use the one provided (if provided)
        if($this->_output["country"] == "" && preg_match("/(". implode('|',$this->_countries)  . ")/i", $this->_string) ){
            preg_match ("/(". implode('|',$this->_countries)  . ")/i", $this->_string, $container["country"]);
            $this->_output["country"] = $container["country"][0];
            $this->_found[] = $this->_output['country'];
            $this->remove($this->_output["country"]);
        }   

        if(preg_match ("/([0-9]{1,})\s+([.\\-a-zA-Z\s*]{1,})/", $this->_string) ){
            preg_match ("/([0-9]{1,})\s+([.\\-a-zA-Z\s*]{1,})/", $this->_string, $container["address"]);
            $this->_output["number"] = $container["address"][1];
            $this->_output["street"] = $container["address"][2];
            $this->_found[] = $this->_output["number"] . " ". $this->_output["street"];
            $this->remove($this->_output["number"]);
            $this->remove($this->_output["street"]);
        }       


        //echo $this->_string;
    }

    /* remove from string in order to make it easier to find missing this */
    protected function remove($string, $case_sensitive = false) {
        $s = ($case_sensitive==false ? "i" : "");
        $this->_string = preg_replace("/".$string."/$s", "", $this->_string);
    }

    public function getNotFound() {
        return array_values(array_filter(array_diff($this->_sections, $this->_found)));
    }

    public function getFound() {
        return $this->_found;   
    }

    /* outputs a readable string with all items found */
    public function toString() {
        $output = $this->getOutput();
        $string = "Original string: [ ".$this->_original_string.' ] ---- New string: [ '. $this->_string. ' ]<br>';
        foreach($output as $type => $data) {
            $string .= "-".$type . ": " . $data. '<br>';    
        }   
        return $string;
    }

    /* return the final output as an array */
    public function getOutput() {
        return $this->_output;  
    }   

}



$array = array();
$array[0] = "123 Main Street, New Haven, CT 06518";
$array[1] = "123 Main Street, New Haven, CT";
$array[2] = "123 Main Street, New Haven,                            CT 06511";
$array[3] = "New Haven,CT 66554, United States";
$array[4] = "New Haven, CT06513";
$array[5] = "06513";
$array[6] = "123 Main    Street, New Haven CT 06518, united states";

$array[7] = "1253 McGill College, Montreal, QC H3B 2Y5"; // google Montreal  / Canada
$array[8] = "1600 Amphitheatre Parkway, Mountain View, CA 94043"; // google CA  / US
$array[9] = "20 West Kinzie St., Chicago, IL 60654"; // google IL / US
$array[10] = "405 Rue Sainte-Catherine Est, Montreal, QC"; // Montreal address shows hyphened street names
$array[11] = "48 Pirrama Road, Pyrmont, NSW 2009"; // google Australia


foreach($array as $string) {
    $a = new Extract($string);

    echo $a->toString().'<br>'; 
}

使用上面代码中的示例,它应输出:

Original string: [ 123 Main Street, New Haven, CT 06518 ] ---- New string: [ , , ]
-state: CT
-city: New Haven
-country: United States
-zip: 06518
-street: Main Street
-number: 123

Original string: [ 123 Main Street, New Haven, CT ] ---- New string: [ , , ]
-state: CT
-city: New Haven
-country: 
-zip: 
-street: Main Street
-number: 123

Original string: [ 123 Main Street, New Haven, CT 06511 ] ---- New string: [ , , ]
-state: CT
-city: New Haven
-country: United States
-zip: 06511
-street: Main Street
-number: 123

Original string: [ New Haven,CT 66554, United States ] ---- New string: [ , , ]
-state: CT
-city: New Haven
-country: United States
-zip: 66554
-street: 
-number: 

Original string: [ New Haven, CT06513 ] ---- New string: [ , ]
-state: CT
-city: New Haven
-country: United States
-zip: 06513
-street: 
-number: 

Original string: [ 06513 ] ---- New string: [ 06513 ]
-state: 
-city: 
-country: 
-zip: 
-street: 
-number: 

Original string: [ 123 Main Street, New Haven CT 06518, united states ] ---- New string: [ , , ]
-state: CT
-city: New Haven
-country: United States
-zip: 06518
-street: Main Street
-number: 123

Original string: [ 1253 McGill College, Montreal, QC H3B 2Y5 ] ---- New string: [ , , ]
-state: QC
-city: Montreal
-country: Canada
-zip: H3B 2Y5
-street: McGill College
-number: 1253

Original string: [ 1600 Amphitheatre Parkway, Mountain View, CA 94043 ] ---- New string: [ , , ]
-state: CA
-city: Mountain View
-country: United States
-zip: 94043
-street: Amphitheatre Parkway
-number: 1600

Original string: [ 20 West Kinzie St., Chicago, IL 60654 ] ---- New string: [ , , ]
-state: IL
-city: Chicago
-country: United States
-zip: 60654
-street: West Kinzie St.
-number: 20

Original string: [ 405 Rue Sainte-Catherine Est, Montreal, QC ] ---- New string: [ , , ]
-state: QC
-city: Montreal
-country: 
-zip: 
-street: Rue Sainte-Catherine Est
-number: 405

Original string: [ 48 Pirrama Road, Pyrmont, NSW 2009 ] ---- New string: [ , , ]
-state: NSW
-city: Pyrmont
-country: Australia
-zip: 2009
-street: Pirrama Road
-number: 48

如果要提取实际存储的值,以便可以使用。您需要致电getOutput()。这将返回一个包含所有必需值的数组。如果我们在列表中取第一个地址并使用此方法输出其值,则应输出:

Array
(
    [state] => CT
    [city] => New Haven
    [country] => United States
    [zip] => 06518
    [street] => Main Street
    [number] => 123
)

请注意,此课程可以大大优化和改进。这是我在一小时内出现的,所以我不能保证它适用于所有类型的输入。实质上,您必须确保用户至少努力使用逗号分隔地址的各个部分。您还需要确保提供大写状态和有效的五位邮政编码。

一些规则

  1. 要提取邮政编码,必须在其旁边提供有效的2字符状态,并附带有效的邮政编码。示例:CT 06510.如果没有状态,只输入五位数字是没有意义的,因为街道号码中也可能有五位数字。 (无法区分两者)。

  2. 只有在按顺序提供数字和单词时才能提取街道和号码。示例:123 Main Street。它也必须用逗号分隔,否则它将捕获数字后面的所有单词。例如,123 Main Street New Haven, CT 06518,代码就是街道和数字为123 Main Street New Haven而不是123 Main Street

  3. 只需输入五位邮政编码即可。

  4. 如果没有提供国家/地区,则会猜测该国家/地区是否提供有效的邮政编码(请参阅上面的邮政编码列表及其各自的国家/地区)。

  5. 它假定不提供连字符(特别是对于城市名称)。这可以在以后修改。 (需要修改正则表达式以适应城市和街道名称的带连字符的单词)。(已修复)

  6. 最重要的是,如果你有时间更改和修改正则表达式并相应地自定义它,你可以做更多的事情。

  7. 我强烈建议您使用表单(如果您还没有),以便轻松捕获输入中提供的地址。它可能会让你的生活变得更轻松。

    快速使用

    $Extract = new Extract("123 Main Street, New Haven, CT 06518");
    $foundValues = $Extract->getOutput();