解决搜索插件乱序问题
7232708273 2023-1-19

function search_keyword_highlight($s, $keyword_arr) {
    foreach($keyword_arr as $key => $keyword) {
        if($key >= 1){
            $chunks = preg_split("/(<span.*?\/span>)/ms", $s, - 1, PREG_SPLIT_DELIM_CAPTURE);
            foreach ($chunks as $c) {
                if (strpos($c, "<span") !== 0){
                    $tmp = $c;
                    $c = str_ireplace($keyword, "<span class="text-danger">".$keyword. "</span>", $c);
                    $s = str_ireplace($tmp, $c, $s);
                }
            }
        }else{
            $s = str_ireplace($keyword, "<span class="text-danger"> ".$keyword. "</span>", $s);
        }
    }
    return $s;
}
最新回复 (0)
全部楼主
返回