Re: regexp novice

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


On Fri, May 18, 2012 at 2:37 AM, Jim Giner <jim.giner@xxxxxxxxxxxxxxxxxx>wrote:

> ok - finally had to come up with my own regexp - and am failing.
>
> Trying to validate an input of a time value in the format hh:mm, wherein
> I'll accept anything like the following:
> hmm
> hhmm
> h:mm
> hh:mm
>
> in a 12 hour format.  My problem is my test is ok'ing an input of 1300.
>
> Here is my test:
>
>  if (0 == preg_match("/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/",$t))
>    return true;
> else
>    return false;
>
> Can someone help me correct my regexp?
>
>
>
I can not correct your regexp. But I must tell you that trying to tweak a
regex for hours is surely **not productive**. If you got any type of text
processing dont always go for regular expression. This problem can be
solved just by simple string parsing.
Here I have done that for you.


function valid_time($time){
        $m  = (int) substr($time, -2);
        $h  = (int) substr($time, 0, -2);
        return ($h>=0 && $h<13 && $m>=0 && $m<60);
}


-- 
Shiplu.Mokadd.im
ImgSign.com | A dynamic signature machine
Innovation distinguishes between follower and leader

[PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [Find Someone]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux