所以我正在尝试编写一个函数,如果它存在于名称中,它将从街道名称中拉出方向。
例如,街道Burnhamthorpe Rd West
会提取单词West
我已使用以下正则表达式完成此操作:
(?:\b(W)\b)|(?:\b(West)\b)
问题是,当我点击以下地址Brown's Line
并使用正则表达式测试南方向时,它会拉出's
(?:\b(S)\b)|(?:\b(South)\b)
我的php看起来如下
private function breakUpStreetNameParts($address)
{
$directions = ["N" => "North", "S" => "South", "E" => "East", "W" => "West"];
$direction = "";
foreach ($directions as $short_dir => $long_dir) {
if (preg_match_all("/(?:\b({$short_dir})\b)|(?:\b($long_dir)\b)/i", $address, $parts)) {
$direction = $parts[0][0];
$patterns = [
"/{$parts[0][0]}/",
"/\s\s/"
];
$replacements = [
"",
" "
];
$address = trim(preg_replace($patterns, $replacements, $address));
}
}
return [
"st_name" => $address,
"st_direction" => $direction
];
}
我如何为此正确使用正则表达式以使其正常工作并忽略撇号作为单词边界?
修改
在下面添加了我的修复程序:)
答案 0 :(得分:1)
我把它整理出来,以下工作完美,即使是以方向开头的街道名称:
public string SmurfName { get; set; }