【发布时间】:2014-07-08 07:44:43
【问题描述】:
我有一个要在字符串 ($text) 中突出显示的单词列表(在数组中)。这是我的代码
$text = " A string with a spans and color highlighted";
$words = array('and','span');
foreach($words as $word){
$patterns[] = '/'.$word.'/i';
}
foreach($words as $word){
$replacements[] = "<span style='color:red;font-weight:bold;'>".$word."</span>";
}
echo preg_replace($patterns, $replacements, $text);
我想替换 $text 中的单词 span 和 color,但结果有所不同,它还替换了 html 标签 span。我该如何克服这个问题。或者我可以有其他选择吗?
您可以在此处重现问题。 http://writecodeonline.com/php/
提前致谢。
【问题讨论】:
标签: php regex preg-replace