...

View Full Version : preg_match extract date from string



dprichard
11-07-2010, 07:37 PM
I need to figure out how to extract a date out of a string. Sometimes it is at the beginning, sometimes it is in the middle with a space on each side. I have my preg_match pulling out the year, but only at the beginning of the string.



preg_match('/^\d{4} /',$title,$matches);


If they put anything before the year it doesn't see the year. I can't quite figure out how to pull the year if it isn't at the beginning of the string. Any help would be great.

Thank you.

Keleth
11-07-2010, 07:48 PM
'/^\d{4} /' means search for 4 digits followed by a space at the beginning of the string. Drop the ^ to have it search for 4 consecutive digits followed by a space anywhere.

dprichard
11-07-2010, 07:53 PM
Thank you that worked. What is the difference between /\d{4} / and /[0-9]{4} /

I get the same result with both.

Keleth
11-07-2010, 07:54 PM
There's no difference. \d means digit, and [0-9] means the digits 0 through 9, which is all of um :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum