2016-05-23 181 views
-1

请告诉如何使正则代码从字符串中查找所有出现的日期,格式如下:Preg match以这种格式查找所有发生的日期26 2016年1月26日2016年1月26日或2016年1月26日

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' 

) 

PLE ase告诉如何使用PHP正则表达式来做同样的事情。

+0

*请告诉如何制作正则表达式代码* =>否。欢迎来到SO,在此之前,您需要发布一个问题**尝试**,失败,然后**尝试更难**,再次失败,然后提出问题并告诉我们**你试过的**。 –

回答

0

使用preg_match_allarray_mapdatestrtotime函数的溶液(I已经修改了初始串中的位得到一个复杂的情况下):

$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 
) 
+0

谢谢@RomanPerekhrest –

+0

@AbhranilMajumder,不客气 – RomanPerekhrest

相关问题