开发者

PHP preg replace only allow numbers

开发者 https://www.devze.com 2023-04-12 00:13 出处:网络
How can I modify this existing preg_replace to only allow 开发者_开发知识库numbers? function __cleanData($c)

How can I modify this existing preg_replace to only allow 开发者_开发知识库numbers?

function __cleanData($c) 
{
    return preg_replace("/[^A-Za-z0-9]/", "",$c);
}


I think you're saying you want to remove all non-numeric characters. If so, \D means "anything that isn't a digit":

preg_replace('/\D/', '', $c)


Try this:

return preg_replace("/[^0-9]/", "",$c);


This should do what you want:

preg_replace("/[^0-9]/", "",$c);


In my case, I want to keep number and '.' only

return preg_replace("/[^0-9.]/", "",$c);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号