PHP - 删除任何字母前的任何数字


PHP - Remove any numbers before any letters

>我正在处理地址,但我只想删除街道号码,例如

123 Fake St

我使用以下正则表达式/[^a-z ]/i'它工作正常并导致

Fake St

但是有时我有这样的地址

M4 Western Distributor Fwy

我将如何保留 M4 部分?因为如果我运行我的正则表达式,它会变成

M Western Distributor Fwy

任何帮助将不胜感激,干杯

尝试

/^[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"

使用

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

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

/'b['d]+'b/

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

$test="123 Fake St";
    $arr=explode(" ",$test);
    if(ctype_digit($arr[0])){
        $test=str_replace($arr[0],"",$test);
    }
echo $test;