Reputation: 325
I have a text Summary for rxd7865 November 13, 2012 to November 13, 2012
.
I want to fetch only date string like November 13, 2012
from above text.
How I can use regular expressions in PHP to do this?
Upvotes: 1
Views: 1242
Reputation: 16107
The regex:
preg_match('/(?<the_date>(January|February|March) [0-9]{2}, 20[0-9]{2})/', $string, $matches);
echo $matches[the_date];
The explanation:
() // are called capture groups or matches
(?<name>) // are named capture groups or named matches
| // separator between a list of alternative matches
[] // is a character class
[0-9] // is a character class that allows only characters from 0 to 9
{} // is a repetition specifier
[]{2} // allows the character class to repeat twice
Upvotes: 2