Re: Invalid endpoint error on regular expression
| From: | Rod Apeldoorn | Date: | Fri, 10 Nov 2000 17:41:31 +0000 |
| Subject: | Re: Invalid endpoint error on regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24674@lists.php.net to get a copy of this message | ||
"Seth Rothberg" <sethmr@bellatlantic.net> wrote in message
news:382066129.973870165716.JavaMail.root@web622-wrb.mail.com...
> Using php 3.x, I'm trying to build a regular expression with data gathered
from
> a submitted form.
>
> The code is in early days. Here it is:
> $dateMask = "$startMonth_[$startDay-$endDay]_$startYear";
>
> $dateMask will be a string that looks something like this:
> 11_[12-18]_2000
>
> Yet when I try to use $dateMask as a pattern for ereg:
> if(ereg($dateMask, $file)) $calendar[] = $file;
>
> I get a warning message that I've got an invalid endpoint in the range.
Looks
> like a valid range to me. What am I missing?
> --Seth
Hey Seth,
Regular Expressions are only really good for checking the format of the
string, and not the data itself. Although it is possible to write an
expression which does what you are looking for, it would be far easier if
you extract the month and work with it in PHP.
The reason why your expression failed (11_[12-18]_2000) was because the
bracket sections are used to match individual characters, not integers.
[0-9] would work because it would match a character valued between 0 and 9,
but [12-18] would not.
Here's a quick code snippet which does what I think you are looking for. It
first verifies the Month_Day_Year format using a regular expression. Then it
splits the data using "_" as the delimiter assigning the day to $fileDay.
Only then does it check to ensure the day is between $startDay and $endDay.
$dateMask = '^((0?[0-9])|(1[0-2]))_(([0-2]?[0-9])|(3[0-1]))_[1-2][0-9]{3}$';
if(ereg($dateMask, $file)) {
list ($fileMonth, $fileDay, $fileYear) = split ('_', $file);
if (($fileDay >= $startDay) && ($fileDay <= $endDay)){
$calendar[] = $file;
}
}
Hope this helps!
---
Rod Apeldoorn
rod@canowhoopass.com
http://www.canowhoopass.com/