[英]Search & Highlight Multi Keywords
When I press 'process' button I want the text highlights the related words on keywords something like this: 当我按下“处理”按钮时,我希望文本突出显示类似这样的关键字上的相关单词:
I try using preg_replace($keyword, $keywords) not work. 我尝试使用preg_replace($ keyword,$ keywords)不起作用。 Any body help? 有身体帮助吗? Thank you ! 谢谢 !
You can do something like this 你可以做这样的事情
<?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);
OUTPUT: OUTPUT:
$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;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.