【问题标题】:How to check variable have a digit at the end or not [duplicate]如何检查变量末尾是否有数字[重复]
【发布时间】:2015-04-17 21:36:42
【问题描述】:

我有 2 个变量 1 我的文件名1 我的文件名 12 .我想检查最后是否有数字。

【问题讨论】:

    标签: php


    【解决方案1】:

    试试这个...

    $test="1 my file name 12";
    $te = preg_match_all("/.*?(\d+)$/", $test, $digit);
    
    if($te>0) {
        echo $test." have following digit in end".$digit[count($digit)-1][0];
    }
    

    结果: 1 my file name 12 have following digit in end 12

    【讨论】:

      【解决方案2】:

      查看下面的简单正则表达式:

      $var = 'my file name 2';
      
      if (preg_match('~\d$~', $var)) {
          echo 'second filename ends with a digit';
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-28
        • 2012-09-30
        • 1970-01-01
        • 2011-12-01
        • 1970-01-01
        • 2015-06-10
        • 2018-08-13
        • 2010-09-23
        相关资源
        最近更新 更多