如何使用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".

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                   ^

I just coded it for myself some minutes ago. It is basically controlling that is between 00:00 and 23:59

edit

preg_match("/(0?\d|1\d|2[0-3]):[0-5]\d/", $line, $matches_time);

You need to be sure that 25:30 is not valid. 24:00 is not valid but 00:00 is valid. this code can make it perfect:

$string='00:34';  
$pattern='%^([0-1][0-9])|([2][0-3]):[0-5][0-9]$%';

if(preg_match($pattern,$string))
    echo '<b>'.$string.'</b> is a valid time in 24 hours format';

If you want to check only 12 hours format, you can use this one:

$string='09:24';  
$pattern='%^([0][0-9])|([1][0-2]):[0-5][0-9]$%';

if(preg_match($pattern,$string))
    echo '<b>'.$string.'</b> is a valid time in 12 hours format';