在自由格式文本中查找日期的最佳方法是什么?用户可以通过多种不同方式在其中放置日期的帖子,例如:
- 7 月 14 日至 15 日
- 7/14 和 7/15
- 7-14 & 7-15
- 周六 14 日和周日 15 日
- 7月14日至15日星期六
等等。正则表达式是我处理这类事情的最佳选择preg_match
吗?我还想搜索是否有两个日期,一个是开始日期,另一个是结束日期,但在我搜索的文本中可能有一个或两个日期。
到目前为止,这是我的 PHP 代码:
$dates1 = '01-01';
$dates2 = 'July 14th & 15th';
$dates3 = '7/14 & 7/15';
$dates4 = '7-14 & 7-15';
$dates5 = 'Saturday 14th and Sunday 15th';
$dates6 = 'Saturday July 14th and 15th';
$regexes = array(
'/\s(1|2|3|4|5|6|7|8|9|10|11|12)\/\d{1,2}/', //finds a date
'/\s(1|2|3|4|5|6|7|8|9|10|11|12)-\d{1,2}/', //finds another date
'%\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])\b%', //finds date format dd-mm or dd.mm
);
foreach($regexes as $regex){
preg_match($regex,$dates,$matches);
}
var_dump($matches);