I have the following piece of PHP code for matching a birth year:
$pattern = "/20\d\d/";
$exp = "BORN ON: Friday, June 11, 2001 ";
$match = preg_match($pattern,$exp, $matches);
echo $matches[0];
This matches "2001"…which is what I am trying to match–the birth year. But this is clearly a hack of a solution and can run into problems if there are other years before $exp for some reason.
How can I include the "BORN ON" information to pick out just the year?
Search also for “BORN ON” and use a capturing group.
I put the Year into a capturing group because of the brackets around it now, you get this group with the
$matches[1](instead of 0). Additionally I bound the year to the end of the string (there can be whitespace\sin between) with the$.