请告诉我们如何使用正则表达式代码从字符串中查找所有出现的日期,格式如下:
26 jan 2016
26th jan 2016
26 january 2016
26th january 2016
26 feb 15
26th feb 15
即:
$string = "Test 26 jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
我想把结果作为一个数组,我将得到:
$res = array (
'2016-01-26',
'2015-02-12',
'2013-01-27'
)
请告诉我们如何使用PHP正则表达式进行相同的操作。
答案 0 :(得分:0)
使用preg_match_all
,array_map
,date
和strtotime
函数的解决方案(我已经修改了初始字符串以获得复杂的情况):< / p>
$string = "Test 26th jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
preg_match_all("/\b\d{2}(?:\w{2})? \w+? \d{2,4}\b/i", $string, $matches);
$dates = array_map(function($d) {
return date('Y-m-d', strtotime($d));
}, $matches[0]);
print_r($dates);
输出: 阵列
(
[0] => 2016-01-26
[1] => 2015-02-12
[2] => 2013-01-17
)