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条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助