douqie1884 2015-07-07 14:02
浏览 20
已采纳

如何使用preg_match验证hh:mm中的时间字符串?

Below are the following possiblities. Time is for 12 hours

 Inputs          Output should as

"05:30"           True
"asasds"          FALSE
"05:30:sads"      FALSE
"ADAS:05:40"      FALSE
"04:30:40"        FALSE

Below is the code that I wrote,

 $value = "05:30" ;
if(!preg_match("/(1[012]|0[0-9]):([0-5][0-9])/", $value)){
    echo "failed"; exit;
}
echo "passed"; exit;

But it prints as passed if I give the $value = "05:30:asdsa". However I need the output to be "failed".

  • 写回答

3条回答 默认 最新

  • dpp42324 2015-07-07 14:04
    关注

    Use anchors to match the start and the end, e.g.

    !preg_match("/^(1[012]|0[0-9]):([0-5][0-9])$/", $value)
                //^ See here                   ^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有偿,学生成绩信息管理系统
  • ¥15 Arduino电机和openmv连接异常
  • ¥15 Arcgis河网分级报错
  • ¥200 java+appium2.1+idea
  • ¥20 请帮我做一个EXE的去重TXT文本
  • ¥15 工价表引用工艺路线,应如何制作py和xml文件
  • ¥15 根据历史数据,推荐问题类型
  • ¥15 需要仿真图,简单的二阶系统实例
  • ¥15 stm32光控照明仿真
  • ¥15 使用人工智能的方法生成满足一定统计参数要求的随机数序列