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.


Need Your Help

Search thought insertions or deletions

git

Using git, is it possible to search on insertions or deletions?

refresh view in navigation controller

iphone ios xcode uitableview

I am making an app where in if I tap on a cell of a table, I get navigated to another view. I am posting some data on that page but when I tap on the back button, the records are not updated. I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.