str_replace
将弄乱页面上任何不吉利的标记,因为它将替换标记或URL内的匹配文本。
你想要的是我给另一个问题的答案的修改版本about highlighting search terms. 变化将是highlight_search_term
作用您只需将其更改为使用$_GET
数据而不是搜索数据。
function highlight_search_term($text){
if(!empty($_GET[\'city\']){
$keys = strip_tags($_GET[\'city\']); // nominal validation
$pattern = \'/<[^>].*?>/i\';
preg_match_all($pattern,$text,$matches);
$placeholders = array();
foreach ($matches[0] as $v) {
$placeholders[] = highlight_search_term_placeholders();
}
$text = preg_replace_callback($pattern,\'highlight_search_term_cb\',$text);
$pattern2 = \'/(\' . $keys .\')/iu\';
$text = preg_replace($pattern2, \' <span class="search-term">\\1</span> \', $text);
$text = preg_replace($placeholders,$matches[0],$text);
}
return $text;
}