2016-05-23 91 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

相關問題