PHP正则表达式-仅过滤器编号 [英] PHP regular expression - filter number only

查看:95
本文介绍了PHP正则表达式-仅过滤器编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来可能是一个很愚蠢的问题,但是我试图确保所提供的字符串为数字/十进制格式,以便以后在PHP的number_format()函数中使用它.

I know this might sound as really dummy question, but I'm trying to ensure that the provided string is of a number / decimal format to use it later on with PHP's number_format() function.

我该怎么做-假设有人在文本字段中输入15:00-我应该使用什么正则表达式和php函数从中删除冒号并使它仅返回有效字符.

How would I do it - say someone is typing 15:00 into the text field - what regular expression and php function should I use to remove the colon from it and make it only return the valid characters.

preg_match()返回数组-因此,除非我对它进行内爆或类似的操作,否则无法将结果传递给number_format().

preg_match() returns array - so I can't pass the result to number_format() unless I implode() it or something like this.

非常感谢您的帮助.

推荐答案

使用 is_numeric intval 可能是在此处验证数字的最佳方法,但是要回答您的问题,您可以尝试使用 preg_replace .本示例删除所有非数字字符:

Using is_numeric or intval is likely the best way to validate a number here, but to answer your question you could try using preg_replace instead. This example removes all non-numeric characters:

$output = preg_replace( '/[^0-9]/', '', $string );

这篇关于PHP正则表达式-仅过滤器编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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