开发者

I need php regular expression for numeric value including "-" and ","

开发者 https://www.devze.com 2023-03-26 17:51 出处:网络
In php is_numeri开发者_运维知识库c function not allow \'-\' and \',\'. This one should work: preg_match(\"#^-?\\d+(,\\d+)?$#\", \"-1,2\", $match);

In php is_numeri开发者_运维知识库c function not allow '-' and ','.


This one should work:

preg_match("#^-?\d+(,\d+)?$#", "-1,2", $match);

Matching one or more digits:

"#\d+#"

Optionally match a comma followed by one or more digits:

"#\d+(,\d+)?#

Optionally match a "-" sign:

"#-?\d+(,\d+)?#"

Allow only this and nothing else:

"#^-?\d+(,\d+)?$#"


is_numeric() does allow negative numbers. I think the problem is just with the comma.

is_numeric( str_replace( ',', '.', $number ) );

See also Converting a number with comma as decimal point to float


try with this expression

^[0-9 ,-]+$
0

精彩评论

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