【问题标题】:PHP Check If String Contains Numbers and Check String LengthPHP检查字符串是否包含数字并检查字符串长度
【发布时间】:2014-03-03 23:13:48
【问题描述】:

我正在尝试检查用户名字符串是否全为数字(但该字段不是数字字段)。如果是这样,我需要通过添加前导 0 使字符串长度为 5 个字符。

我有这个代码:

<?php
$getuname = $_GET['un'];

if (mb_strlen($getuname) <= 1){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 2){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 3){
        $getuname = "0".$getuname;
    }
if (mb_strlen($getuname) <= 4){
        $getuname = "0".$getuname;
    }   
echo $getuname;
?>

上面的代码通过添加零来确保字符串是 5 个字符,但它不是很漂亮,我相信有更好的方法来做到这一点。有人吗?

此外,整个部分需要包含在一个 IF 语句中,该语句首先检查它是否只包含数字。我尝试使用!is_numeric,但这似乎不起作用。我假设是因为它不是数字类型字段。

【问题讨论】:

  • 使用preg_match检查用户名是否只包含数字es1.php.net/preg_match
  • tas9 完全正确,preg_match 是检查这一点的最佳解决方案。 is_int 和 is_numeric 在这种情况下不可靠。
  • 仅供参考:对于超过 100 万次的速度测试,ctype_digit 耗时 0.114777 秒,正则表达式解决方案耗时 0.783449 秒

标签: php numeric


【解决方案1】:

使用str_pad() 函数。像这样:

$getuname = str_pad($_GET['un'], 5, '0', STR_PAD_LEFT);

【讨论】:

    【解决方案2】:
    <?php
    $getuname = $_GET['un'];
    
    
    while (strlen($getuname) < 5) {
        $getuname = '0' . $getuname;
    }
    
    echo $getuname;
    
    ?>
    

    【讨论】:

      【解决方案3】:

      这应该是一个干净的解决方案:

      $getuname = $_GET['un'];
      if(preg_match('/^\d+$/',$getuname)){
          echo sprintf('%05d', $getuname);
      }else{
          // incorrect format
      }
      

      【讨论】:

      • 这里不需要使用正则表达式。
      • 为什么不呢?检查“我正在尝试检查用户名字符串是否全为数字”的首选解决方案是什么
      【解决方案4】:

      可以使用is_numeric 来检查数字字段,变量是否为字符串并不重要。 See example here.

      然后你可以简单地使用str_pad()填充

      if(!is_numeric($test))
          echo $test . ' is not a number!';
      else
          echo str_pad($test, 5, 0, STR_PAD_LEFT);
      

      编辑: 正如 flixer 指出的那样,is_numeric() 实际上不会执行您具体要求的操作(检查字符串是否仅包含数字,即没有句点、逗号、破折号等被认为是“一个数字”)。在这种情况下,请改用ctype_digit()

      $test_number = '123.4';
      $test_number2 = '12345';
      
      echo ctype_digit($test_number) ? $test_number . ' is only digits' : $test_number . ' is not only digits';
      echo ctype_digit($test_number2) ? $test_number2 . ' is only digits' : $test_number2 . ' is not only digits';
      
      // output:
      // 123.4 is not only digits
      // 12345 is only digits
      

      这里的关键是avoid regex when you have better tools to do the job.

      再补充一点,ctype_digit() 可能会在您传入整数变量时返回 false:(来自 PHP 手册的示例)

      ctype_digit( '42' ); // true
      ctype_digit( 42 ); // false - ASCII 42 is the * symbol
      

      这没关系,具体取决于您使用它的情况。在您的情况下,您正在验证一个 $_GET 变量,该变量始终是一个字符串,因此不会影响您。

      文档:

      OP 在这里,这就是全部。像魅力一样工作......

         if (ctype_digit($getuname) == true) {
          $getuname = str_pad($getuname, 5, 0, STR_PAD_LEFT);
          }
      

      【讨论】:

      • "is_numeric" 不是检查字符串是否仅包含数字的好解决方案,因为它会在 '5.1'、'-3' 和 '1e7' 等情况下返回 true
      【解决方案5】:

      试试这个:

      <?php
      $getuname = $_GET['un'];
      if(ctype_digit($getuname))
           $getuname = str_repeat("0", 5-strlen($getuname)) . $getuname;
      ?>
      

      希望它对你有用。

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 2013-11-20
        • 2011-11-09
        • 2013-05-18
        • 2021-12-14
        • 2013-10-26
        • 1970-01-01
        相关资源
        最近更新 更多