Jump to content

[SOLVED] preg_match timestring


RichardRotterdam

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.