douzachan4578 2015-09-16 02:53
浏览 106
已采纳

正则表达式检查以空格和总长度开头的字符串10 PHP

I wrote PHP code to check the string with regular expression. Every string should start with white space and total length should be exactly 10.

$str = ' 123456789';
if(preg_match('/^([ 0-9]){10}+$/', $str)){
      echo 'true';
}
    echo 'false';

I have expected the following results when I change the $str variable. But using the above regular expression, I only get the first one right.

$str = ' 1234567890'; //true
$str = '1234567890'; // false 
  • 写回答

3条回答 默认 最新

  • dongzhizhai4070 2015-09-16 02:59
    关注

    If you truly mean white space then use \s as your white space because it grabs tabs as well. Also, It looks like you only want digits. If so use:

    '/^\s\d{9}$/'
    

    If not then use:

    '/^\s.{9}$/'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?