PHP代码删除除数字之外的所有内容

时间:2022-02-27 07:28:49

I'm trying to remove everything from a string but just numbers (0-9).

我试着从一个字符串中除去所有的东西,只是数字(0-9)。

I thought this would work..

我以为这行得通。

echo preg_replace("[^0-9]","",'604-619-5135');

But it echos "604-619-5135". What am I missing???

但《“604-619-5135”。我错过什么呢? ? ?

3 个解决方案

#1


203  

Try this:

试试这个:

preg_replace('/[^0-9]/', '', '604-619-5135');

preg_replace uses PCREs which generally start and end with a /.

preg_replace使用PCREs,通常以/开头和结尾。

#2


85  

This is for future developers, you can also try this. Simple too

这是为未来的开发人员准备的,您也可以尝试一下。简单的过

echo preg_replace('/\D/', '', '604-619-5135');

#3


10  

You would need to enclose the pattern in a delimiter - typically a slash (/) is used. Try this:

您需要将模式包含在分隔符中——通常使用一个斜杠(/)。试试这个:

echo preg_replace("/[^0-9]/","",'604-619-5135');

#1


203  

Try this:

试试这个:

preg_replace('/[^0-9]/', '', '604-619-5135');

preg_replace uses PCREs which generally start and end with a /.

preg_replace使用PCREs,通常以/开头和结尾。

#2


85  

This is for future developers, you can also try this. Simple too

这是为未来的开发人员准备的,您也可以尝试一下。简单的过

echo preg_replace('/\D/', '', '604-619-5135');

#3


10  

You would need to enclose the pattern in a delimiter - typically a slash (/) is used. Try this:

您需要将模式包含在分隔符中——通常使用一个斜杠(/)。试试这个:

echo preg_replace("/[^0-9]/","",'604-619-5135');