Forum Moderators: coopster

Message Too Old, No Replies

Using preg_match() to specify a range of numbers.

Getting the right regex to match *only* 2004 through 2006

         

maerk

2:30 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



How can I use preg_match() to check if a number is between two values (specifically 2004 and $currentYear, which is equal to date(Y))?

The pattern must be between 2004 and $currentYear, I want to return an error for any year out of that range, so I can't simply use [0-9]{4}.

Here is the code I'm using at the moment:

if ( preg_match("/2004¦2005¦2006¦all/i", $_GET['year']) ) {
$year = strip_tags($_GET['year']);

} else {
die("Please enter a year in the form of four digits between 2004 and $currentYear, or enter 'all' to see all years.");
}

coopster

2:47 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$pattern = "/^200[4-6]$/";

The circumflex and dollar sign anchor the begin and end of the string (so the form doesn't allow something like 12004 or 20056 as a year entry).

[edited by: coopster at 2:52 pm (utc) on Mar. 15, 2006]

maerk

2:49 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



Is there a way to do this using my $currentYear variable? Basically I'd like to be able not to have to edit the pattern every year...

Something like "/[2004-$currentYear]¦all/i"

That example doesn't work, by the way...

[edited by: maerk at 2:53 pm (utc) on Mar. 15, 2006]

coopster

2:52 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was just editing my post when you responded ;) Here you go ...

Of course, there are a number of other ways to do this as well. One might be to create a range of years based on the current year and then check to see if the year is in that array:

if (in_array [php.net]($year, range [php.net](date [php.net]('Y') - 2, date('Y')))) { 

maerk

2:57 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



Wouldn't that match only 2 years away from date(Y)? I'd still have to change the "2" every year, kinda defeats the object :)

I'm sure there must be a simple way in regex?

coopster

3:01 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry, didn't realize 2004 was the static number here. Just change the begin range to 2004. Yes, you can also do it with a regular expression you just need to use the date function to get the current year.

coopster

3:11 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You know, a simple if statement here would probably be the easiest now that I understand what you are looking for. Just check to make sure the year entered falls between the two in your predefined range:
if ($year >= 2004 && $year <= date('Y')) {

You might want to do some other stuff, like make sure it is numeric, only contains 4 digits, etc. but you can see where this is much easier than trying to get it nailed down with a regex.

maerk

3:19 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



if ($year >= 2004 && $year <= date('Y')) {

Yeah, that would be best, but $year can also be "all" -- which isn't numeric! Can you nest conditions? I've tried it before, without much success:

if ( ($year >= 2004 && $year <= date('Y')) or ($year == "all") ) {

Is there a correct way of doing this?

coopster

3:27 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, that's it and yes it should work. Nesting logic can get sticky sometimes so the easiest way to get it right is to break it down in to pieces and put it all back together, matching opening and closing parenthesis. Understanding Operator Precedence [php.net] is quite helpful too.

maerk

3:45 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



Yes, that was it :D It's working just as I wanted it to, thanks very much!