How to extract year using preg_match
I have textarea where users enter details, I want to extract year from the textarea. The problem is users enter year in different ways.
some users input dates as 1999 or '99 or september 99. how can I use a single regex string to extract year.
Answers
This should work for extracting the mentioned dates out of text:
preg_match_all('/(^|\s+)(\d{4}|\'\d{2}|(january|february|march|april|may|june|july|august|september|october|november|december) \d{2})(\s+|$)/i', $text, $matches);
This came to my mind, too (it gives the user a little more freedom):
preg_match_all('/(^|\s+)((january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)?(\s|\s?\')(\d{2}|\d{4}))(\s+|$)/i', $text, $matches);
All of the above would be easier when parsed with multiple expressions. Why do you need a single one?
If you simply want to parse a string that contains nothing else than this, you should use PHP's strtotime()-function.