将从内容中查找字符串并替换为 HTML 标记的正则表达式

Regex which will find string from content and replace with HTML tag

我有这样的内容。

"Test with the dummy content. Another dummy content."

我想要正则表达式,如果我传递特定的字符串和特定的 html 标签,那么它将用标签替换字符串。例如

function strReplace($content, $string, $tag)
{
    // $output = regex function ...

}

如果我调用类似 strReplace($content, 'dummy', 'b');

的方法

输出应该是:

"Test with the <b>dummy</b> content. Another <b>dummy</b> content."

提前致谢

您正在搜索 preg_splithttp://php.net/manual/en/function.preg-split.php

您可以拆分正则表达式,接收片段,然后在第一个元素之后和最后一个元素之前插入您的标签。这可能(取决于您的上下文)仅 str_split.

也是可能的

寻找解决方案。

尝试了一些东西,它起作用了。使用像

这样的正则表达式
// $tag = '<b>'; or $tag = 'b';
$tag = str_replace('<', '', str_replace('>', '', $tag));

$output = preg_replace(array('/'.$string.'/'), array('<'.$tag.'>'.$string.'</'.$tag.'>'), $content);

感谢您的帮助,我们随时欢迎您提出建议。