php PHP从数字字符串中删除逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9334879/
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
PHP remove commas from numeric strings
提问by user1082428
In PHP, I have an array of variables that are ALL strings. Some of the values stored are numeric strings with commas.
在 PHP 中,我有一个所有字符串的变量数组。一些存储的值是带逗号的数字字符串。
What I need:
我需要的:
A way to trim the commas from strings, and ONLY do this for numeric strings. This isn't as straightforward as it looks. The main reason is that the following fails:
一种从字符串中修剪逗号的方法,并且仅对数字字符串执行此操作。这并不像看起来那么简单。主要原因是以下失败:
$a = "1,435";
if(is_numeric($a))
$a = str_replace(',', '', $a);
This fails because $a = "1435"
is numeric. But $a = "1,435"
is not numeric. Because some of the strings I get will be regular sentences with commas, I can't run a string replace on every string.
这失败,因为$a = "1435"
是数字。但$a = "1,435"
不是数字。因为我得到的一些字符串将是带逗号的常规句子,所以我无法对每个字符串运行字符串替换。
采纳答案by Kenaniah
Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
未测试,但可能类似 if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
回答by JJJ
Do it the other way around:
反过来做:
$a = "1,435";
$b = str_replace( ',', '', $a );
if( is_numeric( $b ) ) {
$a = $b;
}
回答by Jayela
The easiest would be:
最简单的是:
$var = intval(preg_replace('/[^\d.]/', '', $var));
or if you need float:
或者如果你需要浮动:
$var = floatval(preg_replace('/[^\d.]/', '', $var));
回答by kaushi
Try this .this worked for me
试试这个。这对我有用
number_format(1235.369,2,'.','')
number_format(1235.369,2,'.','')
if you use number_format like this
number_format(1235.369,2)
answer will be 1,235.37
如果你使用 number_format 这样的
number_format(1235.369,2)
答案将是1,235.37
but if you use like below
但如果你使用如下
number_format(1235.369,2,'.','')
answer will be 1235.37
number_format(1235.369,2,'.','')
答案将是1235.37
it's removing the "," of "1,235.37"
它正在删除“1,235.37”的“,”
回答by orrd
It sounds like the ideal solution for what you're looking for is filter_var()
:
听起来您正在寻找的理想解决方案是filter_var()
:
$a = filter_var($a, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
(Note that it's using FILTER_VALIDATE_FLOAT instead of FILTER_VALIDATE_INT because that one doesn't currently have a FILTER_FLAG_ALLOW_THOUSAND option).
(请注意,它使用的是 FILTER_VALIDATE_FLOAT 而不是 FILTER_VALIDATE_INT,因为它当前没有 FILTER_FLAG_ALLOW_THOUSAND 选项)。
回答by Ryan
function cleanData($a) {
if(is_numeric($a)) {
$a = preg_replace('/[^0-9,]/s', '', $a);
}
return $a;
}
回答by Flaxious
If you want to remove commas from numbers inside a string that alsocontains words, the easiest way I think would be to use preg_replace_callback:
如果要从还包含单词的字符串中的数字中删除逗号,我认为最简单的方法是使用preg_replace_callback:
Example:
例子:
$str = "Hey hello, I've got 12,500 kudos for you, spend it well"
$str = "Hey hello, I've got 12,500 kudos for you, spend it well"
function cleannr($matches)
{
return str_replace("," , "" , $matches["nrs"]);
}
$str = preg_replace_callback ("/(?P<nrs>[0-9]+,[0-9]+)/" , "cleannr" , $str);
Output:
输出:
"Hey hello, I've got 12500 kudos for you, spend it well"
“喂喂,我有12500个赞给你,好好花吧”
In this case the pattern (regex) differs from the one given in the accepted answer since we don't want to remove the other commas (punctuation).
在这种情况下,模式(正则表达式)与接受的答案中给出的模式不同,因为我们不想删除其他逗号(标点符号)。
If we'd use /[0-9,]+/
here instead of /[0-9]+,[0-9]+/
the output would be:
如果我们在/[0-9,]+/
这里使用而不是/[0-9]+,[0-9]+/
输出将是:
"Hey hello I've got 12500 kudos for you spend it well"
“嘿,你好,我有 12500 个荣誉,因为你花得很好”