如何在PHP中用html标记替换%

如何在PHP中用html标记替换%,php,Php,我有以下PHP代码: $fullString = "a %sample% word go %here%"; 我想用html标记(和)替换%。这是更换后我想要的: $fullString ="a <span style='color:red'>sample</span> word go <span style='color:red'>here</span>"; $fullString=“示例单词转到此处”; 使用PHP函数如str_replac

我有以下PHP代码:

$fullString = "a %sample% word go %here%";
我想用html标记(
)替换%。这是更换后我想要的:

$fullString ="a <span style='color:red'>sample</span> word go <span style='color:red'>here</span>";
$fullString=“示例单词转到此处”;
使用PHP函数如
str_replace、preg_replace
等,我应该做些什么来实现这个结果。? 谢谢。

你可以

$string = "a %sample% word go %here%";
echo preg_replace('~%(.*?)%~', '<span style="color:red">$1</span>', $string);

像这样的?谢谢斯克鲁勒。。。它起作用了!:)
a <span style="color:red">sample</span> word go <span style="color:red">here</span>