Jump to content

Archived

This topic is now archived and is closed to further replies.

LostOblivion

What the h#ll?!

Recommended Posts

Hey. First time post here I think. I'm making this calendar and I'm trying to parse a time format from an input field - when I suddenly hit a wall.

What I'm trying to do here is to get the hours and minutes off of a string such as

2:50
02:50
2 50
02 50
2.50
02.50
250
0250
2
02

All of the above can be entered by the user and I've come up with a regex I think works to do this job. (All of the above can be read as 2 hours and 50 minutes by a human right? (Except for the bottom two which are 2 hours.) And I'm sure this regex works since I've tested it with ereg.)

[code]
$regex = "^([0-9]?[0-9])(:|\.| )?([0-9][0-9])?$";
if ( preg_match( $regex, $_POST['user_field_goes_here'], $matches ) )
die( "Ok...it works." );
else
die( "Nope. Still no good result. =(" );
[/code]

But when I try to get the groups into $matches array the function returns false?!?!?! Heck it does so even without the $matches array as an argument.

The problem is I always get the sad smiley, ok! =S Is there something weird about this function or what?

I mean. Try running this:

[code]
$regex = "^([0-9]?[0-9])(:|\.| )?([0-9][0-9])?$";
if ( ereg( $regex, $_POST['user_field_goes_here'] ) )
die( "Jay." );
else
die( "No." );
[/code]

Tell me what?! Ahh, you got the "Jay." eh? =) It's weird.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.