RichardRotterdam Posted September 10, 2008 Share Posted September 10, 2008 I never used regular expressions much since i didnt found the need to really use it and there were always other ways to achieve the samething without using it. however in a lot of cases its quite usefull. I'm trying to validate a string to see if it is a valid time. the string should allow "00:00" to "24:00" I have the following <?php $pattern ="/([0-2][0-9])+[0-5][0-9])$/"; if (preg_match($pattern, $time_val)) { echo("valid string"); } ?> this works buttttttttttttttttttttttt....... this also allows the value "99:00" the minutes i think i have but the hours is trickier how can fix it so it allows the numbers till 24? Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/ Share on other sites More sharing options...
JasonLewis Posted September 10, 2008 Share Posted September 10, 2008 Give this a shot: $pattern ="/(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))[0-5]{1}[0-9]{1})$/"; Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638243 Share on other sites More sharing options...
RichardRotterdam Posted September 10, 2008 Author Share Posted September 10, 2008 thanks that nearly worked it still accepted 24:31 but i fixed that by changing the 4 into a 3 result <?php $pattern ="/(([0-1]{1}[0-9]{1})|([2]{1}[0-3]{1}))[0-5]{1}[0-9]{1})$/"; if (preg_match($pattern, $time_val)) { echo("valid string"); } ?> Other then having a solution i want to understand what it actually does. some things that are unclear to me 1. does the pipeline"|" in the patternt mean OR? 2. what does {1} mean in the pattern Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638285 Share on other sites More sharing options...
thebadbad Posted September 10, 2008 Share Posted September 10, 2008 A bit late, but gonna post anyway Aren't 00:00 and 24:00 the same moment (or 24 hours apart)? And you forgot a caret (^) at the start of the pattern. Also, it can be done simpler than ProjectFear's code (BTW, it also allows 24:59); all the {1}s aren't necessary. 00:00-24:00: $pattern = '~^((([01]\d)|([2][0-3])):[0-5][0-9])|24:00$~D'; and 00:00-23:59: $pattern = '~^(([01]\d)|([2][0-3])):[0-5][0-9]$~D'; Notes: Yes, the pipe means OR, and {1} means "exactly one of the previous character" ([0-9]{1} would be one digit between 0 and 9). But {1} isn't necessary, since a character class matches only one character, when nothing else is specified. You can also specify a range with e.g. {1,3} (between 1 and 3 characters). Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638302 Share on other sites More sharing options...
thebadbad Posted September 10, 2008 Share Posted September 10, 2008 To explain further, \d is equal to using [0-9]. (([01]\d)|([2][0-3])) matches either 00-19 or 20-23, thus not allowing 29:xx as an example. ^ matches the start of the string, and $ the end (i.e. we're validating the whole string, not parts of it). The D modifier at the end of the pattern makes sure that $ matches the end of the string only, and not also a new line (read here). Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638305 Share on other sites More sharing options...
thebadbad Posted September 10, 2008 Share Posted September 10, 2008 BTW, http://www.regular-expressions.info/ is a great resource if you want to go further with learning regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638307 Share on other sites More sharing options...
RichardRotterdam Posted September 10, 2008 Author Share Posted September 10, 2008 thanks a bunge thats something i can work with Quote Link to comment https://forums.phpfreaks.com/topic/123591-solved-preg_match-timestring/#findComment-638320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.