Whether you're drawing customers into your restaurant for dinner, or friends over to your house for dessert, these tips will whet everyone's appetite.
You are here
Home › Blogs › Guy Paddock's blog › Generating PCRE Regular Expressions from Date Format Strings in PHPGenerating PCRE Regular Expressions from Date Format Strings in PHP
Have you ever found yourself wishing that it was as easy to write a regular expression to match a time/date string as it is to generate a time/date string with the PHP date() function?
Do you find yourself in need of a quick and easy way to "tokenize" time/date strings like "September 7th, 2010" into values like "9", "7", and "2010"?
Well, I'm proud to announce that we here at RedBottle have the solution for your problems, and it's called DateFormatRegexGenerator.
Without further ado, here's an example to illustrate its power:
$dateString = "September 7th, 2010";
$regexGenerator = DateFormatRegexGenerator::getInstance();
/* Generates the following regular expression:
*
* /^(?:January|February|March|April|May|June|July|August|September|October|November
* |December) (?:[1-9]|[1-2][0-9]||3[0-1])(?:st|nd|rd|th), (?:[0-9]{4})$/
*/
$regex = $regexGenerator->generateRegex('F jS, Y', TRUE);preg_match($regex, $dateString, $matches);
print_r($matches);
This will print:
Array
(
[0] => September 7th, 2010
)
The DateFormatRegexGenerator class supports converting any date format string that can be passed to the PHP date() function into a regular expression that can be used to match date/time strings of that format. This makes generation of date-and-time-related regular expressions trivial.
In addition, CapturingDateFormatRegexGenerator, a sub-class of DateFormatRegexGenerator, can be used to generate "capturing" regular expressions that capture each part of the date string, making it easy to both validate and tokenize date/time strings.
Here's an example that's similar to the one above, but more powerful:
$dateString = "September 7th, 2010";
$regexGenerator = CapturingDateFormatRegexGenerator::getInstance();
/* Generates the following regular expression:
*
* /^(January|February|March|April|May|June|July|August|September|October|November
* |December) ([1-9]|[1-2][0-9]||3[0-1])(st|nd|rd|th), ([0-9]{4})$/
*/
$regex = $regexGenerator->generateRegex('F jS, Y', TRUE);preg_match($regex, $dateString, $matches);
print_r($matches);
This will produce the following output:
Array
(
[0] => September 7th, 2010
[1] => September
[2] => 7
[3] => th
[4] => 2010
)
You can get these handy utility classes, along with their corresponding PHPUnit unit tests, from our website. The code is licensed under version 3 of the Lesser GNU General Public License.
If you find any bugs or have any questions, feel free to post a comment.
RBD News
New RedBottle Website
ROCHESTER, NY - Red Bottle Design, LLC has launched its new corporate website running on Drupal 7.
Latest Blog Posts
- 1 of 14
- ››
Social Media
- RedBottleDesign W'hoo! Love the new RedBottle Design Office. t.co/LWvEcn1R
- RedBottleDesign t.co/FNwqApQi t.co/FapBkC21
- RedBottleDesign We have a great feeling about 2012... stay tuned!
- RedBottleDesign RedBottle Design wishes everyone a safe and happy Holiday Season.
- RedBottleDesign New blog entry: Java-inspired Enumerated Types in PHP - t.co/Mzix0Vh0
