Skip to content

Instantly share code, notes, and snippets.

@overtrue
Last active April 1, 2019 17:40
Show Gist options
  • Save overtrue/42a607a692d3efc5897762f56a16d826 to your computer and use it in GitHub Desktop.
Save overtrue/42a607a692d3efc5897762f56a16d826 to your computer and use it in GitHub Desktop.
匹配文本中的日期部分
<?php
$cases = [
'明年,1月1日下午2:30,test',
'二零一七年,1月1日下午2:30,test',
'今年,12月1日下午2:30,test',
'嘎嘎嘎,1月1日下午2:30,test',
'2016年,1月1日下午2:30,test',
'20161月1日下午2:30,test',
'20161月1日上午2:30,test',
'20161月1日上午00:30,test',
'20161月1日临晨01:30,test',
'20161月1日临晨12:30,test',
'20161月1日晚上12:30,test',
'208881月1日下午2:30,test',
'17年,1月1日下午2:30,test',
'16年,1月1日下午2:30,test',
'16年,12月31日2点整,test!!2333',
'111年1月1日下午2:30,test',
'年12月3日下午22:10,test!0',
',12月3日下午22:10,test!0',
'2016年,明天下午2:01,test',
'2017年今天下午2:30,test',
'2017年,后天下午2:30,test',
'2017年"$split"2月01日08:15,hellohello~~1234',
'明,下午2:30,tmp',
'明,上午01:01,tmp',
'明,上午11:09,tmpðŸŽtmp',
'明,上午13:00',
'12月9号,13:30,tmp',
];
foreach ($cases as $case) {
$matched = getMatched($case);
echo "\n{$case}: " .json_encode($matched, JSON_UNESCAPED_UNICODE)."\n\n";
}
function matchDate($string)
{
$regex = "/(?<year>20\d{2})?(?<has_year>年)?[\s,,]*(?<month>\d{1,2}月)(?<date>(?:\d{1,2}[日号])|明天|后天|今天)[\s,,]*((?<hour_prefix>上午|下午|临晨|晚上|早上)?(?<hour>\d{1,2}))(?<min>点(整|半|\d{1,2}分)?|:\d{1,2})/ui";
preg_match($regex, $string, $matched);
if ((empty($matched['year']) && !empty($matched['has_year'])) || empty($matched['min'])) {
return null;
}
$replacement = [
'month' => [
'月' => '',
],
'date' => [
'日' => '',
'号' => '',
'今天' => date('d'),
'明天' => date('d', strtotime('+1 day')),
'后天' => date('d', strtotime('+2 day')),
],
'hour_prefix' => [
'早上' => 'AM',
'上午' => 'AM',
'下午' => 'PM',
'临晨' => 'AM',
'晚上' => 'PM',
],
'min' => [
':' => '',
'点整' => '00',
'点半' => '30',
'分' => '',
],
];
foreach ($matched as $key => $value) {
if (is_numeric($key)) {
unset($matched[$key]);
}
if (isset($replacement[$key])) {
$matched[$key] = str_replace(array_keys($replacement[$key]), $replacement[$key], $value);
}
}
if (!empty($matched['hour_prefix']) && $matched['hour_prefix'] == 'PM' && $matched['hour'] < 12) {
$matched['hour'] += 12;
}
if ($matched['month'] > 12 || $matched['date'] > 31 || $matched['hour'] > 24 || $matched['min'] > 60) {
return null;
}
unset($matched['has_year'], $matched['hour_prefix']);
if (empty($matched['year'])) {
$matched['year'] = date('Y');
}
return $matched;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment