翻译美国街道&;二次缩写成完整的长度版本


Translate US street & secondary abbreviations into full length version

这是列表;我这样做是为了"规范化"地址的数据集,以便于查找。

我试过使用strtr()和str_ireplace(),但效果不好。下面是一组较短的测试列表。

<?php
function street_abbreviations_regex($input) {
  $list = array(
    ' ave'  => ' avenue',
    ' blvd' => ' boulevard',
    ' cir'  => ' circle',
    ' ct'   => ' court',
    ' expy' => ' expressway',
    ' fwy'  => ' freeway',
    ' ln'   => ' lane',
    ' pky'  => ' parkway',
    ' rd'   => ' road',
    ' sq'   => ' square',
    ' st'   => ' street',
    ' tpke' => ' turnpike',
    ' n'    => ' north',
    ' e'    => ' east',
    ' s'    => ' south',
    ' w'    => ' west',
    ' ne'   => ' northeast',
    ' se'   => ' southeast',
    ' sw'   => ' southwest',
    ' nw'   => ' northwest',
  );
//   $input = strtr(strtolower($input), $list);
  $input = str_ireplace(array_keys($list), array_values($list), strtolower($input));
  $regex_street = (preg_replace("/[^A-Za-z0-9]/", "", $input));
  return $regex_street;
?>

输入

echo street_abbreviations_regex('10 E Union St.') . " <br>'n";
echo street_abbreviations_regex('10 E Union Street') . " <br>'n";

strtr()的输出

10eastunionsoutht
10eastunionsouthtreet

str_ireplace()的输出

10eastunionsouthtreet
10eastunionsouthtreetreet

我在一家名为SmartyStreets的公司工作,我们在那里进行地址解析、标准化等……我要说的是,你要做的任务实际上非常复杂。我从经验中知道!

请相信我,地址有多种形式和大小,而不是列出所有类型的输入——有效和无效——它们会胜过任何正则表达式;并且准确地标准化输出并不容易

美国邮政已经认证了少数供应商使用其官方数据执行地址规范化。查看CASS认证服务。您可以使用LiveAddress API(免费)开始搜索。它真的很容易与PHP一起使用(因为LiveAddress会返回一个JSON字符串,PHP会进行本机解析)。

如果有任何进一步的问题,我很乐意亲自回答。