SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: simple regex

  1. #1
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
    if(!== preg_match($pattern$subject)){
        
    #no match
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $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.

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the ^$ signs back in where you had them, like /^...$/

    ^ means start
    $ means end
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am done and upgraded it as per my need.Thanks for the help !!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •