将日期转换为新格式

时间:2017-07-12 21:02:46

标签: php regex

我有一个包含大量日期的文档,总是用标记包装,并且始终采用特定格式。

$text = '...<dt>31 DEC 1793</dt>... ...<dt>14 JAN 1934</dt>...';

我正在尝试将此文本替换为包含星期几:

$text = '...<dt>Tuesday, 31 DEC 1793</dt>... ...<dt>Sunday, 14 JAN 1934</dt>...';

现在我正在尝试使用preg_replace来实现这一点,但它只是给了我当前日期。

$text = preg_replace('/<dt>(\d{1,2} [A-Z]{3} \d{4})<\/dt>/i', "<dt>".date('l', strtotime("$1")).", $1</dt>", $text);

似乎日期函数只运行一次,而不是每次更换一次。我怎么能做这个工作?

1 个答案:

答案 0 :(得分:2)

您需要在回调中运行$text = '...<dt>31 DEC 1793</dt>... ...<dt>14 JAN 1934</dt>...'; $text = preg_replace_callback( '/<dt>(\d{1,2} [A-Z]{3} \d{4})<\/dt>/i', function ($matches) { $date = $matches[1]; return "<dt>".date('l', strtotime($date)).", ".$date."</dt>"; }, $text ); // $text = '...<dt>Tuesday, 31 DEC 1793</dt>... ...<dt>Sunday, 14 JAN 1934</dt>...'; ------------------------------------------------------ | CustomerID | FirstName | LastName | ------------------------------------------------------ | INTEGER PRIMARY KEY | CHAR(15) | CHAR(15) | ------------------------------------------------------ 函数:

INSERT INTO CUSTOMER (CustomerID, FirstName, LastName)
      VALUES (:vcustid, 'Abe', 'Lincoln');