I am processing addresses, but I just want to remove the Street Number, eg
123 Fake St
I use the following regular expression /[^a-z ]/i'
which works fine and results in
Fake St
However sometimes I have addresses such as
M4 Western Distributor Fwy
How would I keep the M4 part? Because if I run my regular expression it turns into
M Western Distributor Fwy
Any help would be appreciated, cheers
Try
/^[0-9 ]+(?=[^\d]+)/i
This matches all numbers that is followed by anything other than numbers, test:
$subject = '123 Fake St';
var_dump(preg_replace('/^[0-9 ]+(?=[^\d]+)/i', '', $subject));
$subject = 'M4 Western Distributor Fwy';
var_dump(preg_replace('/^[0-9 ]+(?=[^\d]+)/i', '', $subject));
Output:
string(7) "Fake St"
string(26) "M4 Western Distributor Fwy"
Use
/\b[^a-z ]+\b/i
as your regex instead. This will match any occurrence of one or more non-letters that are bounded by a word boundary. Actually, if you only want to remove numbers you should use
/\b[\d]+\b/
Some times non regex methods are also worthy
$test="123 Fake St";
$arr=explode(" ",$test);
if(ctype_digit($arr[0])){
$test=str_replace($arr[0],"",$test);
}
echo $test;