php - How to extract year using preg_match -
i have textarea users enter details, want extract year textarea. problem users enter year in different ways.
some users input dates 1999
or '99
or september 99
. how can use single regex string extract year.
this should work extracting 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 mind, (it gives user 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 above easier when parsed multiple expressions. why need single one?
if want parse string contains nothing else this, should use php's strtotime()
-function.
Comments
Post a Comment