gpt4 book ai didi

PHP:检查输入是否为有效数字的最佳方法?

转载 作者:IT老高 更新时间:2023-10-28 12:01:20 34 4
gpt4 key购买 nike

检查输入是否为数字的最佳方法是什么?

  • 1-
  • +111+
  • 5xf
  • 0xf

这类数字不应该是有效的。只有像这样的数字:123、012 (12),正数应该是有效的。这是我当前的代码:

$num = (int) $val;
if (
preg_match('/^\d+$/', $num)
&&
strval(intval($num)) == strval($num)
)
{
return true;
}
else
{
return false;
}

最佳答案

ctype_digit正是为此目的而 build 的。

关于PHP:检查输入是否为有效数字的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418461/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com