簡體   English   中英

正則表達式在php中檢查時間時不起作用

[英]regular expression not working when check time in php

下面是我用於檢查變量是否為時間的代碼

function isTime($time) {

    if (preg_match("/^([1-2][0-3]|[01]?[1-9]):([0-5]?[0-9]):([0-5]?[0-9])$/", $time))
            {
        return true;
            echo "<script>alert('is a time');</script>";
            }
         else {
            echo "<script>alert('not a time');</script>";
           return false;
           }
         }

當值$time=00:08:33這樣的表達式失敗,並且像$time=01:08:33這樣的$time=01:08:33正常工作,請修改此表達式

您需要將第一個捕獲組內的[01]?[1-9]模式更改為[01]?[0-9] ,以便它也匹配數字00 [1-9]匹配此范圍內的所有數字( 1到9 ), [0-9]匹配所有0到9的數字。

^([1-2][0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$
                   ^
                   |

演示

if (preg_match("/^([1-2][0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$/", $time))
{
 ......
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM