当我按下“处理”按钮时,我希望文本突出显示关键字上的相关字词:
我尝试使用preg_replace($ keyword,$ keywords)无效。有人帮吗?谢谢!
答案 0 :(得分:1)
你可以做这样的事情
<?php
$str="Hello World You are on StackOverflow";// Your large text
$arrMatchWords=array("World","on");// Provide all your matching text.
foreach($arrMatchWords as $k=>$v)
{
$str=str_replace($v,"<span style='background:#ffff00'>".$v."</span>",$str);
}
echo ($str);
<强>输出:强>
答案 1 :(得分:0)
$keywords = array("computers" => NULL,"paper" => NULL, "rocket" => NULL);
$text = preg_split('/\s/', $my_text_string);
foreach($text as $key => $value)
{
if(array_key_exists($value, $keywords)) {
echo $value . "\n";
$text[$key] = '<span style="background:yellow">'. $value . '</span>';
}
}
$my_text_string = implode(' ', $text);
echo $my_text_string;
?>