So I'm trying to write a function that pulls out the direction out of a street name if it exists in the name.
For instance the street Burnhamthorpe Rd West would pull out the word West
I have done that with the following regex:
(?:\b(W)\b)|(?:\b(West)\b)
The problem is when I hit the following address Brown's Line and use the regex for testing the South direction, it pulls out the 's
(?:\b(S)\b)|(?:\b(South)\b)
My php looks as follows
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
];
}
How do i do the regex for this so that it works correctly and ignore's the apostrophe as a word boundary?
EDIT
Added my fix to below :)
North RoadorWest Boulevard, named after Mrs. North and Mr. West\s) rather than a word break (\b)? However, I think you have a bigger issue in that your premise of extracting "North" "South" "East" and "West" is going to result in false positives, even if you just take the word on its own. I know of places named "West End" and "North End", etc, and roads leading to them called "West End Road". You can't take the "West" out of that without destroying its meaning.strpos,nevermind the issue raised by Marc above