从字符串中删除所有字符,留下数字

时间:2022-09-13 13:17:49

Hay, i have a string like this:

干草,我有一个像这样的字符串:

v8gn5.8gnr4nggb58gng.g95h58g.n48fn49t.t8t8t57

I want to strip out all the characters leaving just numbers (and .s)

我想删除只留下数字(和.s)的所有字符

Any ideas how to do this? Is there a function prebuilt?

任何想法如何做到这一点?有预制的功能吗?

thanks

谢谢

4 个解决方案

#1


51  

$str = preg_replace('/[^0-9.]+/', '', $str);

replace substrings that do not consist of digits or . with nothing.

替换不包含数字或子句的子串。一无所有。

#2


6  

preg_replace('/[^0-9.]/', '', $string);

#3


1  

$input = 'some str1ng 234';
$newString = preg_replace("/[^0-9.]/", '', $input);

#4


0  

To satisfy my curiosity I asked about the speed of the proposed answers and as shown in preg_replace speed optimisation/ it is (much) faster to use str_replace() than preg_replace().

为了满足我的好奇心,我询问了建议答案的速度,并且如preg_replace速度优化中所示/使用str_replace()比使用preg_replace()更快。

So you might want to use str_replace() instead.

所以你可能想要使用str_replace()代替。

#1


51  

$str = preg_replace('/[^0-9.]+/', '', $str);

replace substrings that do not consist of digits or . with nothing.

替换不包含数字或子句的子串。一无所有。

#2


6  

preg_replace('/[^0-9.]/', '', $string);

#3


1  

$input = 'some str1ng 234';
$newString = preg_replace("/[^0-9.]/", '', $input);

#4


0  

To satisfy my curiosity I asked about the speed of the proposed answers and as shown in preg_replace speed optimisation/ it is (much) faster to use str_replace() than preg_replace().

为了满足我的好奇心,我询问了建议答案的速度,并且如preg_replace速度优化中所示/使用str_replace()比使用preg_replace()更快。

So you might want to use str_replace() instead.

所以你可能想要使用str_replace()代替。