字符串中所有以*开头的单词都应该是粗体。 这是我到目前为止所尝试过的:
$string = "This is *a string to *check *bold";
echo preg_replace('/*(\w+)/', '<b>\1</b>', $string);
这给了我错误:
This is *a string to *check *bold Warning: preg_replace(): Compilation failed: nothing to repeat at offset 0 in...
最佳答案
你有错误的preg_replace()
正则表达式
*
是正则表达式中的特殊字符,应该转义。我使用 #
作为分隔符以获得更好的可见性。
检查下面的代码:
$string = "This is *a string to *check *bold";
echo preg_replace('#\*(\w+)#', '<b>$1</b>', $string);
关于php - 使以 * 开头的单词加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317889/