2

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

4

3 回答 3

1

利用

/\b[^a-z ]+\b/i

作为你的正则表达式。这将匹配以单词边界为界的一个或多个非字母的任何出现。实际上,如果您只想删除数字,您应该使用

/\b[\d]+\b/
于 2012-05-28T06:54:54.650 回答
1

尝试

/^[0-9 ]+(?=[^\d]+)/i

这匹配后跟除数字以外的任何数字的所有数字,测试:

$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));

输出:

string(7) "Fake St"
string(26) "M4 Western Distributor Fwy"
于 2012-05-28T06:58:47.690 回答
1

有时非正则表达式方法也值得

$test="123 Fake St";
    $arr=explode(" ",$test);
    if(ctype_digit($arr[0])){
        $test=str_replace($arr[0],"",$test);
    }
echo $test;
于 2012-05-28T07:01:36.410 回答