我在 Stackoverflow 上搜索了一些对此的答案,并尝试了多种方法来使这项工作没有成功。所以我希望外面的天才能够帮助我。
我正在从一个数据库中检索文本,该文本中包含日期/时间 (dd/mm/yy hh:mm:ss
) 我想将正则表达式找到的所有实例加粗这种格式的日期。
例如:
09/09/2013 16:42:01 Australia/West (Fake Name):
09/09/2013 15:44:01 (alert) alert stage 2
09/09/2013 15:38:55 Australia/West (Fake Name): 2 of the at Store have come back online.
09/09/2013 15:33:17 Australia/West (Fake Name): Store is having issues with their all 3. loading". Investigating.
09/09/2013 15:30:02 Australia/West (Fake Name):
看起来像这样:
09/09/2013 16:42:01 Australia/West (Fake Name):
09/09/2013 15:44:01 (alert) alert stage 2
09/09/2013 15:38:55 Australia/West (Fake Name): 2 of the at Store have come back online.
09/09/2013 15:33:17 Australia/West (Fake Name): Store is having issues with their all 3. loading". Investigating.
09/09/2013 15:30:02 Australia/West (Fake Name):
我已将此正则表达式用于日期/时间:
preg_match_all('/^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/',$text,$matches);
使用此代码:
foreach ($matches as $res)
{
$replaceStr = "<strong>" . $res . "</strong>";
str_replace($res, $replaceStr, $text);
}
但它似乎只在文本中找到 1 个匹配项,所以我需要一点帮助。
最佳答案
不需要 foreach
循环:
preg_replace('/^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/m','<strong>$0</strong>', $text);
一次完成所有匹配和替换。
关于php - 在文本中查找并替换日期/时间并将其设为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18841802/