如何爲每個搜索詞打包html標籤,如突出顯示單詞。我在這裏嘗試了我的代碼,但沒有任何改變。問題在哪裏,還是有其他好的方法?謝謝。php如何爲每個搜索詞打包html標籤,
<?php
$sentence = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
$search = 'php counterparts';//search words
$str = explode(' ',$search);//explode every search word
for($i=0;$i<10;$i++){
if($str[$i]!=''){ //make sure if $str[$i] is not empty, do $newstr.
$newstr .= '\'<b>'.$str[$i].'</b>\','; //wrap <b> tag for search words
}
}
$newstr = substr($newstr, 0, strlen($newstr)-1);//remove last common, combine a array
$new = str_ireplace(array($str),array($newstr),$sentence);
echo $new;
?>
你能更好地解釋什麼是你正在試圖做的?你的代碼雖然很受評論,但並不能完全解釋事情。你想要達到什麼輸出? – JRSofty