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

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

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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向