Расширяя на этот ответ , как насчет того, чтобы использовать это для поиска дат (или вещей, которые, по крайней мере, похожи на даты) в тексте, а затем попытаться проанализировать их:
\b # match a word boundary
(?: # either...
(?: # match the following one to three times:
(?: # either
\d+ # a number,
(?:\.|st|nd|rd|th)* # followed by a dot, st, nd, rd, or th (optional)
| # or a month name
(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)
)
[\s./-]* # followed by a date separator or whitespace (optional)
){1,3} # do this one to three times
| # or match a "colloquial" date and capture in backref 1:
(to(?:day|ni(?:te|ght)|morrow)|next\s+(?:week|month|year))
)
\b # and end at a word boundary.
Итакесли у вас есть совпадение и обратная ссылка $1
пуста, то, вероятно, найдена литеральная дата;если $1
не пусто, он находит дату типа «сегодня» или «на следующей неделе».Конечно, это будет работать только с датами в тексте на английском языке и, вероятно, не будет очень надежным.
if (preg_match(
'%\b # match a word boundary
(?: # either...
(?: # match the following one to three times:
(?: # either
\d+ # a number,
(?:\.|st|nd|rd|th)* # followed by a dot, st, nd, rd, or th (optional)
| # or a month name
(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)
)
[\s./-]* # followed by a date separator or whitespace (optional)
){1,3} # do this one to three times
| # or ...
(?:to(?:day|ni(?:te|ght)|morrow)|next\s+(?:week|month|year))
)
\b # and end at a word boundary.%ix',
$subject, $regs)) {
$result = $regs[0];
$colloq = $regs[1]; // don't know what happens if $1 didn't participate in the match, though.
} else {
$result = "";
}