【问题标题】:How can I validate an unsigned number in PHP?如何在 PHP 中验证无符号数?
【发布时间】:2011-04-03 10:28:31
【问题描述】:

我不知道如何处理这个问题,我已经尝试了对我来说最明显的解决方案,但到目前为止,没有一个完全令人满意。我一定忽略了一些非常简单的事情。

我有一个输入文本类型的表单:

<input type="text" name="album_id">

我想验证输入,以便用户只输入无符号整数...

$required = array(); // required or invalid input

$tmp = trim($_POST['album_id']);

if (!is_int((int)$tmp)) {
   $required['album'] = 'The album id must contain a positive numeric value only.';
}

到目前为止,我一直在使用 !is_numeric($tmp),但用户可以输入 9.2 或 '1e4' 并且它会验证...所以它不起作用。

我也尝试过!is_int((int)$tmp),但由于某种原因,那个不起作用(也许应该,但我做错了......)。 我试过ctype_digit 没有成功。 我可能忽略了一些东西,但不确定是什么。

如何在 php 中验证无符号数?没有浮点数、负数等……只有一个简单的无符号数(1 到 n)。

【问题讨论】:

    标签: php validation


    【解决方案1】:

    如果你想检查某个变量是否只包含数字(这似乎是你想要的,在这里),你可能不得不选择ctype_digit()


    不确定你尝试了什么,但这样的东西应该可以工作:

    $tmp = trim($_POST['album_id']);
    if (ctype_digit($tmp)) {
        // $tmp only contains digits
    }
    

    【讨论】:

    • 确实...我已经像这样放置了 NOT 符号 !ctype_digit($tmp) ,它似乎工作正常!我认为文档显示 $integer = 42; 时让我失望了// false 我忘记了默认情况下输入是字符串而不是整数....谢谢
    【解决方案2】:

    filter_var() 函数是完成这里工作的正确工具。

    这是一个过滤器,它只对无符号整数或无符号整数字符串返回非假:

    $filteredVal = filter_var($inputVal, 
                              FILTER_VALIDATE_INT, 
                              array('options' => array('min_range' => 0)));
    

    这是documentation on filters

    例子:

    <?php
    
    $testInput = array(
                "zero string" => "0",
                "zero" => 0,
                "int" => 111,
                "string decimal" => "222",
                "empty string" => "",
                "false" => false,
                "negative int" => -333,
                "negative string decimal" => "-444",
                "string octal" => "0555",
                "string hex" => "0x666", 
                "float" => 0.777,
                "string float" => "0.888",
                "string" => "nine"
             ); 
    
    foreach ($testInput as $case => $inputVal)
    {
        $filteredVal = filter_var($inputVal, 
                                  FILTER_VALIDATE_INT, 
                                  array('options' => array('min_range' => 0)));
    
        if (false === $filteredVal)
        {
            print "$case (". var_export($inputVal, true) . ") fails\n";
        } 
        else
        { 
            print "$case (". var_export($filteredVal, true) . ") passes\n";
        }
    }
    

    输出:

    zero string (0) passes
    zero (0) passes
    int (111) passes
    string decimal (222) passes
    empty string ('') fails
    false (false) fails
    negative int (-333) fails
    negative string decimal ('-444') fails
    string octal ('0555') fails
    string hex ('0x666') fails
    float (0.777) fails
    string float ('0.888') fails
    string ('nine') fails
    

    【讨论】:

    • 注意 32 位限制:2147483647 通过检查“整数 >0”。 2147483648 失败。添加引号似乎没有任何帮助。
    【解决方案3】:

    你可以使用preg_match():

    if(preg_match('/^[\\d+]$/', $tmp) == 0)
        $required['album'] = 'The album id must ...';
    

    请注意,这不会进行正范围检查(例如,超过整数的最大有效值)。

    编辑: 除非您想进行更复杂的检查(例如需要其他特殊字符),否则请使用 Pascal MARTIN 的解决方案,因为我猜它为此用途提供了更好的性能。

    【讨论】:

      【解决方案4】:
      if (preg_match('!^[1-9][0-9]*$!',$tmp)) {
      

      【讨论】:

      • "0" 和类似 "0123" 的东西仍然是有效的整数值。我不认为任何人会期望这些数字被读取为八进制数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多