PHP remove commas from numeric strings(PHP 从数字字符串中删除逗号)
问题描述
在 PHP 中,我有一个变量数组,它们都是字符串.一些存储的值是带逗号的数字字符串.
In PHP, I have an array of variables that are ALL strings. Some of the values stored are numeric strings with commas.
我需要什么:
一种从字符串中修剪逗号的方法,并且仅对数字字符串执行此操作.这并不像看起来那么简单.主要原因是以下失败:
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);
这失败了,因为 $a = "1435"
是数字.但是 $a = "1,435"
不是数字.因为我得到的一些字符串将是带逗号的常规句子,所以我无法对每个字符串运行字符串替换.
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.
推荐答案
未测试,但可能类似于 if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
这篇关于PHP 从数字字符串中删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 从数字字符串中删除逗号


基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01