2013-11-22 105 views
0

當我按下「過程」按鈕,我想要的文字突出關鍵字像這樣的相關詞:搜索和突出顯示多關鍵詞

enter image description here

我嘗試使用了preg_replace($關鍵字,$關鍵字)不工作。任何身體幫助?謝謝 !

回答

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); 

OUTPUT:

enter image description here

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; 
?>