php - 在文本中查找并替换日期/时间并将其设为粗体

标签 php regex

我在 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/

相关文章:

javascript - 使用正则表达式提取 JavaScript 中给定模式的字符串

regex - 如何用一些文本替换 VIM 中的捕获组 2

php - magento 显示错误 :500 Internal server Error

php - 改进 MySQL LIKE 运算符

php - Laravel - 在 whereHas Closure 内动态访问模型属性

javascript - 字符串中允许有空格,但不能在第一个或最后一个位置

python - 用于在逗号分隔的字符串中查找 float 的正则表达式

Javascript将字符串与模式匹配并在匹配的字符串中插入一些文本

PHP - 查找目录是否有子项的最快方法?

php - 在sql中存储html元素的有效方法