I have a string like this:
some text August 22, 2016
I want to get the date only - august 22, 2016
.
the date is always in the end of the string.
I tried something like this... but no success...
$date2 = preg_match('/\b(.*?)\b&\/\d{2},\b\/\d{4}$/', $string, $date2);
/\b(.*?)\b&\/\d{2},\b\/\d{4}$/
You may use
'~\p{L}+\s*\d{2},\s*\d{4}$~'
See the regex demo
Details:
\p{L}+
- 1 or more letters\s*
- 0+ whitespaces\d{2}
- 2 digits,
- a comma\s*
- 0+ whitespaces\d{4}
- 4 digits$
- end of string.Note that to precise this further, you may want to replace \p{L}
with an alternation group enumerating all months, like (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|Jule?|Aug(?:ust)?|Sep(?:ember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)
that can further be improved/enhanced. To match XXth and XXIst centuries only, you may replace \d{4}
with (?:20|19)\d{2}
.