正则表达式 - 剥离非数字,并删除分,如果有的话 [英] Regex - Strip non numeric and remove cents if any

查看:186
本文介绍了正则表达式 - 剥离非数字,并删除分,如果有的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用PHP开发一个项目,我需要一些正则表达式的帮助。我希望能够输入一个用户输入的货币价值,并去除所有非数字和小数位/美分。



'2.000,00'至'2000'

'$ 2.000,00'至'2000'

'2abc000'至'2000'

'2.000'to 2000



(我使用非美元货币格式)



我该怎么做?我会很感激这个帮助 - 谢谢你们可以这样做:

解决方案

pre $ $ str = preg_replace('/ [^ 0-9,] |,[0-9] * $ /','',$ str);


I'm currently working on a project in PHP and I'm in need of some Regex help. I'd like to be able to take a user inputted monetary value and strip all non numeric and decimal places/cents.

Ex:

'2.000,00' to '2000'
'$ 2.000,00' to '2000'
'2abc000' to '2000'
'2.000' to 2000

(I'm using non US currency formatting)

How can I do this? I'd appreciate the help - Thanks

解决方案

You can do:

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

这篇关于正则表达式 - 剥离非数字,并删除分,如果有的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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