php 从字符串中删除非数字字符(句点和逗号除外)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4949279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove non-numeric characters (except periods and commas) from a string
提问by user485783
If I have the following values:
如果我有以下值:
$var1 = AR3,373.31
$var2 = 12.322,11T
How can I create a new variable and set it to a copy of the data that has any non-numeric characters removed, with the exception of commas and periods? The values above would return the following results:
如何创建一个新变量并将其设置为删除了任何非数字字符(逗号和句点除外)的数据副本?上述值将返回以下结果:
$var1_copy = 3,373.31
$var2_copy = 12.322,11
回答by John Parker
You could use preg_replaceto swap out all non numeric characters and the comma and period/full stopas follows:
您可以使用preg_replace替换所有非数字字符以及逗号和句点/句号,如下所示:
<?php
$testString = '12.322,11T';
echo preg_replace('/[^0-9,.]/', '', $testString);
?>
回答by Bryan Way
I'm surprised there's been no mention of filter_varhere for this being such an old question...
我很惊讶这里没有提到filter_var因为这是一个如此古老的问题......
PHP has a built in method of doing this using sanitization filters. Specifically, the one to use in this situation is FILTER_SANITIZE_NUMBER_FLOAT
with the FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND
flags. Like so:
PHP 有一个内置的方法来使用消毒过滤器来做到这一点。具体来说,在这种情况下FILTER_SANITIZE_NUMBER_FLOAT
使用的是FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND
标志。像这样:
$numeric_filtered = filter_var("AR3,373.31", FILTER_SANITIZE_NUMBER_FLOAT,
FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
echo $numeric_filtered; // Will print "3,373.31"
It might also be worthwhile to note that because it's built-in to PHP, it's slightlyfaster than using regex with PHP's current libraries (albeit literally in nanoseconds).
可能还值得注意的是,因为它是 PHP 内置的,所以它比在 PHP 的当前库中使用正则表达式稍快(尽管实际上是在纳秒内)。
回答by mopo922
Simplest way to truly remove all non-numeric characters:
真正删除所有非数字字符的最简单方法:
echo preg_replace('/\D/', '', $string);
\D
represents "any character that is not a decimal digit"
\D
表示“任何不是十进制数字的字符”
回答by Adeel
You could use filter_var
to remove all illegal characters except digits, dot and the comma.
您可以使用filter_var
删除除数字、点和逗号之外的所有非法字符。
- The
FILTER_SANITIZE_NUMBER_FLOAT
filter is used to remove all non-numeric character from the string. FILTER_FLAG_ALLOW_FRACTION
is allowing fraction separator" . "
- The purpose of
FILTER_FLAG_ALLOW_THOUSAND
to get comma from the string.
- 该
FILTER_SANITIZE_NUMBER_FLOAT
过滤器用于去除从字符串中的所有非数字字符。 FILTER_FLAG_ALLOW_FRACTION
允许分数分隔符" . "
FILTER_FLAG_ALLOW_THOUSAND
从字符串中获取逗号的目的。
Code
代码
$var1 = '12.322,11T';
echo filter_var($var1, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
Output
输出
12.322,11
To read more about filter_var()and Sanitize filters
阅读更多关于filter_var()和Sanitize 过滤器的信息
回答by andymnc
If the request is to remove all (and not only one) non numerical char, maybe the previous can be written like this in a very simple way (but you could write a function to pass the string and make it return the needed value without chars.. anyway..):
如果请求是删除所有(而不仅仅是一个)非数字字符,也许可以用一种非常简单的方式将前面的字符写成这样(但您可以编写一个函数来传递字符串并使其返回所需的值而无需字符.. 反正..):
<?php
$String1 = 'AR3,373.31';
$String2 = '12.322,11T';
echo preg_replace('/[^0-9,.]+/i', '', $String1);
echo preg_replace('/[^0-9,.]+/i', '', $String2);
?>
回答by Andrew
If letters are always in the beginning or at the end, you can simply just use trim...no regex needed
如果字母总是在开头或结尾,您可以简单地使用修剪...不需要正则表达式
$string = trim($string, "a..zA..Z"); // this also take care of lowercase
"AR3,373.31" --> "3,373.31"
"12.322,11T" --> "12.322,11"
"12.322,11" --> "12.322,11"
回答by jonasdev
Same answer as middaparka but remove the ,.
与 middaparka 相同的答案,但删除,.
$unformatted_phone = "phone 122-3222223.ext 442";
echo preg_replace("/[^0-9]/", "", $unformatted_phone);