使用PHP分解除数字以外的所有字符

使用PHP分解除数字以外的所有字符,php,numbers,explode,Php,Numbers,Explode,我有一个网址,我正试图爆炸出它的所有内容,只得到其中的数字。。。它看起来是这样的:www.url.com/blalb/5435/blabla url总是不同的,所以我需要分解除数字以外的所有内容。 它必须使用PHP。尝试以下方法: $url; #this contains your url string $matches = array(); #this will contain the matched numbers if(preg_match_all

我有一个网址,我正试图爆炸出它的所有内容,只得到其中的数字。。。它看起来是这样的:www.url.com/blalb/5435/blabla

url总是不同的,所以我需要分解除数字以外的所有内容。 它必须使用PHP。

尝试以下方法:

$url;                  #this contains your url string
$matches = array();    #this will contain the matched numbers

if(preg_match_all('/\d+/', $url, $matches)) {
    $numbers = implode('', $matches[0]);
    echo "numbers in string: $numbers";
}
这使用带有
preg\u match\u all
的正则表达式来匹配字符串中的所有数字组,将每组放入
$matches[0]
数组中。然后您可以简单地
将这个数字组数组内爆成一个字符串

例如,如果
$url
包含
'www.url.com/blalb/5435/blabla/0913'
,则输出将是
字符串中的数字:54350913

如果您只想匹配URL中的第一组数字,请使用
preg\u match
而不是
preg\u match\u all

if(preg_match('/\d+/', $url, $matches)) {
    $numbers = implode('', $matches);
    echo "numbers in string: $numbers";
}

如果你想匹配字符串中的一组特定数字(除了第一个),你需要一个更复杂的正则表达式。

你想与数字进行preg_匹配吗?我不介意,如果URL中根本没有数字,我希望用户会看到一个错误。
如果(!preg_match(“[0-9]#,$URL))
。是的,但我仍然需要查看替换代码,以便能够将数字用作变量。请阅读preg_match()的第三个参数: