Welcome Guest, Not a member yet? Register   Sign In
Strange regex behaviour
#1

[eluser]alboyd[/eluser]
Hi Guys,

I have no idea about regex really - never really tried to figure it out too much. I can guess how the regex below is supposed to work and I think I found this snippet online somewhere.

All along so far I have been testing with a time of 20:30 and it has worked but I have just found out that other valid HH:MM times are not working?

Here is some testing code I am running.
Code:
function index()
    {
        echo '01:50' . "<br>";
        $this->valid_time('01:50');
        
        echo "<br>";
        echo '03:45' . "<br>";
        $this->valid_time('03:45');
        
        echo "<br>";
        echo '05:40' . "<br>";
        $this->valid_time('05:40');
        
        echo "<br>";
        echo '07:35' . "<br>";
        $this->valid_time('07:35');
        
        echo "<br>";
        echo '09:30' . "<br>";
        $this->valid_time('09:30');
        
        echo "<br>";
        echo '11:25' . "<br>";
        $this->valid_time('11:25');
        
        echo "<br>";
        echo '18:55' . "<br>";
        $this->valid_time('18:55');
        
        echo "<br>";
        echo '20:55' . "<br>";
        $this->valid_time('20:55');
        die();
        
        //$this->admin_panel();
    }
    
    function valid_time($str) {
            
        if (ereg("([0-23]{2}):([0-59]{2})", $str)) {
            $arr = split(":", $str);
            $hh = $arr[0];
            $mm = $arr[1];
            echo 'regex is fine';
            if (is_numeric($hh) && is_numeric($mm)) {
               return true;
            } else {
                return false;
            }
        } else {
            echo 'regex fails';
            return false;
        }
    }

Here are the results:

Code:
01:50
regex is fine
03:45
regex is fine
05:40
regex fails
07:35
regex fails
09:30
regex fails
11:25
regex is fine
18:55
regex fails
20:55
regex is fine

Anyone got an idea of what is wrong - or does anyone know how I can validate a time is HH:MM.

Thanks!


Messages In This Thread
Strange regex behaviour - by El Forum - 10-06-2009, 04:29 AM
Strange regex behaviour - by El Forum - 10-06-2009, 04:42 AM
Strange regex behaviour - by El Forum - 10-06-2009, 05:09 AM
Strange regex behaviour - by El Forum - 10-06-2009, 05:26 AM
Strange regex behaviour - by El Forum - 10-06-2009, 07:57 AM
Strange regex behaviour - by El Forum - 10-06-2009, 01:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB