doupi1532
2019-02-03 19:37
浏览 246
已采纳

从特定格式的完整地址字符串中提取城市和州

I have a set of addresses stored as individual strings. Here are possible values of those strings:

"Staples Center, 555 Test Drive, Los Angeles, CA 98112"
"555 Test Drive, Los Angeles, CA 98112"
"Los Angeles, CA"
"Los Angeles, CA 98112"
"Los Angeles"

The dates will always follow this format, where it could contain a venue name, street address, city, state, and zip

I would like to dynamically extract the city and state only from these strings:

$addresses_array = array(
    "Staples Center, 555 Test Drive, Los Angeles, CA 98112",
    "555 Test Drive, Los Angeles, CA 98112",
    "Los Angeles, CA",
    "Los Angeles, CA 98112",
    "Los Angeles"
);

foreach ($addresses_array as $address) {
    if (strpos($address, ',') !== false) {
        // extract the city and state
    }
}

Given the pattern of the address values, is there something I can do to extract the city and state based on the commas?

What I can see is that the city will always be the last comma in the string..

图片转代码服务由CSDN问答提供 功能建议

我有一组地址存储为单独的字符串。 以下是这些字符串的可能值:

 “Staples Center,555 Test Drive,Los Angeles,CA 98112”
“555 Test Drive,Los Angeles,CA 98112”\  n“Los Angeles,CA”
“洛杉矶,加利福尼亚州98112”
“洛杉矶”
   
 
 

日期将始终遵循此格式, 包含场地名称,街道地址,城市,州和邮编

我想从这些字符串中动态提取城市和州:

  $ addresses_array = array(
“Staples Center,555 Test Drive,Los Angeles,CA 98112”,
“555 Test Drive,Los Angeles,CA 98112”,
“洛杉矶,CA”,
  n“Los Angeles,CA 98112”,
“洛杉矶”
); 
 
foreach($ addresses_array as $ address){
 if(strpos($ address,',')!== false){  
 //提取城市和州
} 
} 
   
 
 

鉴于地址值的模式,我可以做些什么来提取城市 并且以逗号为基础进行陈述?

我能看到的是,这座城市将会如此 方式是字符串中的最后一个逗号..

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dplht39359 2019-02-03 19:51
    已采纳

    If I read it correct then you want the two last items (if available).
    Use explode and then implode back the last two items.

    $addresses_array = array(
        "Staples Center, 555 Test Drive, Los Angeles, CA 98112",
        "555 Test Drive, Los Angeles, CA 98112",
        "Los Angeles, CA",
        "Los Angeles, CA 98112",
        "Los Angeles"
    );
    
    foreach ($addresses_array as &$address) {
        $arr = explode(", ", $address);
        $address = implode(", ", array_slice($arr, -2));
    }
    
    var_dump($addresses_array);
    

    Output:

    array(5) {
      [0]=>
      string(21) "Los Angeles, CA 98112"
      [1]=>
      string(21) "Los Angeles, CA 98112"
      [2]=>
      string(15) "Los Angeles, CA"
      [3]=>
      string(21) "Los Angeles, CA 98112"
      [4]=>
      &string(11) "Los Angeles"
    }
    

    https://3v4l.org/Gqcid

    If you don't want the zip, then you can explode the last item on space if $arr count is more than one.

    foreach ($addresses_array as &$address) {
        $arr = explode(", ", $address);
        if(count($arr) >1){
            $arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0];
            $address = implode(", ", array_slice($arr, -2));
        }
    }
    

    Output:

    array(5) {
      [0]=>
      string(15) "Los Angeles, CA"
      [1]=>
      string(15) "Los Angeles, CA"
      [2]=>
      string(15) "Los Angeles, CA"
      [3]=>
      string(15) "Los Angeles, CA"
      [4]=>
      &string(11) "Los Angeles"
    }
    

    https://3v4l.org/uKvke

    打赏 评论
  • doushajian2018 2019-02-03 19:40

    you can use explode and count function together

    
    $addresses_array = array(
        "Staples Center, 555 Test Drive, Los Angeles, CA 98112",
        "555 Test Drive, Los Angeles, CA 98112",
        "Los Angeles, CA",
        "Los Angeles, CA 98112",
        "Los Angeles"
    );
    
    
    foreach ($addresses_array as $address) {
        $addr = explode(",",$address);
        switch (count($addr))
        {
            case 1:
            case 2:
                echo  $addr[0];
                break;
            case 3:
                echo  $addr[1];
                break;
            case 4:
                echo  $addr[2];
                break;
    
        }
    }
    
    打赏 评论

相关推荐 更多相似问题