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

想要24小时preg_match

匹配时间格式:

01:00, 02:00, 13:00,
 1:00,  2:00, 13:01,
23:59, 15:00,
00:00,  0:00,
14:34:43, 01:00:00

不匹配时间格式:

24:00             # hour is out of range [0-23]
 12:60             # minute is out of range [00-59]
  0:0              # invalid format for minute, at least 2 digits
 13:1              # invalid format for minute, at least 2 digits
  0:00:0           # invalid format for seconds, at least 2 digits
101:00             # hour is out of range [0-23]

但是在

var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25'));    // OK
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:25'));    // KO

我得到错误

 Warning: preg_match() [function.preg-match]: Compilation failed: unmatched parentheses at offset 18 in C:\xampp\htdocs\march\admin\test.php on line 8
bool(false) 0:00 - No
4

1 回答 1

1

您缺少一个左括号。

var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25'));
//                     ^ here
var_dump(preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25'));
于 2013-09-27T04:01:54.310 回答