Simple regex

Hi,

I am trying to validated entered time in HH:MM:SS [24 hour formatting]

if(!preg_match(“/^(\d\d):(\d\d):(\d\d)$/”,$this->post[‘start_time’]))

it goes correct with some validation but it fails for "00:00:00a " as I already specified here as 2 digit but still something is wrong in above regex.

Kindly let me know where am i wrong?

Given the following…

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.

I’d be a little bit more specific and explicitly check for no match, rather than no match or error.


<?php
if(1 !== preg_match($pattern, $subject)){
    #no match
}
?>

$end_time=‘00:00:00kkkkkkk’;
$r=preg_match(“/[0-9]{2}:[0-9]{2}:[0-9]{2}/”,$end_time);
print_r($r);

return true!!

any idea how to correct the preg_match expression to validation hh:mm:ss correctly.

Put the ^$ signs back in where you had them, like /^…$/

^ means start
$ means end

I am done and upgraded it as per my need.Thanks for the help !!