从字符串中删除非数字字符(句号和逗号除外) [英] Remove non-numeric characters (except periods and commas) from a string

查看:90
本文介绍了从字符串中删除非数字字符(句号和逗号除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我具有以下值:

 $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

推荐答案

您可以使用

You could use preg_replace to swap out all non numeric characters and the comma and period/full stop as follows:

<?php
    $testString = '12.322,11T';
    echo preg_replace('/[^0-9,.]/', '', $testString);
?>

这篇关于从字符串中删除非数字字符(句号和逗号除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆