/** * 自動(dòng)識(shí)別關(guān)鍵詞方法 * @param String $text 需要查詢的文本 * @param String $keysStr 用來標(biāo)記的關(guān)鍵詞字符串 * @param Int $similar 可以插入的關(guān)鍵詞相似度 默認(rèn)60% * @return Array */ function searchKeys($text = "",$keysStr = "",$similar = ""){ //關(guān)鍵詞相似度 $similar = $similar == null ? 60 : $similar; //組裝特殊字符,并替換 $Exp = str_replace(array(":","。",'"',"/","-","_","=","~","`","(",")","*","&","^","%","$","#","@","!",":",":","、","“",".","”",";","】","【","[","]","|",'\/'," "," ","'",""","<",">","?","/","]","[","!","@","#","$","%","^","&","*","(",")","_","+","=","-","/","*","-","+",".","`","~",";",":","'",""","|","\"),"",strip_tags($text)); $Exps = str_replace(",",",",$Exp); //將切割的文字組裝成數(shù)組 $textArr_1D = explode(",",$Exps); //將內(nèi)容轉(zhuǎn)換成二維數(shù)組 $textArr_2D = array(); foreach($textArr_1D as $val){ $textArr_2D[]['text'] = $val; } //去除關(guān)鍵詞的最后一位 $keysStr = substr($keysStr,0,strlen($keysStr)-1); //切割關(guān)鍵詞成一維數(shù)組 $keysArr_1D = explode(",",$keysStr); //轉(zhuǎn)換成二維數(shù)組 $keysArr_2D = array(); foreach ($keysArr_1D as $val) { $keysArr_2D[]['keys'] = $val; } //開始匹配關(guān)鍵詞 foreach ($textArr_2D as $t_k => $t_v) { foreach ($keysArr_2D as $k_k => $k_v) { //判斷關(guān)鍵詞不為空 if($k_v['keys'] != ""){ //根據(jù)文本相似度 if(similar_text($t_v['text'],$k_v['keys'],$percent)){ //當(dāng)相似度大于等于**時(shí)插入到數(shù)組 if($percent >= $similar){ $data[]['keys'] = $k_v['keys'].$percent."%"; } } //不區(qū)分大小寫尋找相同字符 if(stristr($t_v['text'],$k_v['keys']) != false){ $data[]['keys'] = $k_v['keys']; } //區(qū)分大小寫尋找相同字符 if(strpos($t_v['text'],$k_v['keys']) != false){ $data[]['keys'] = $k_v['keys']; } } } } return array_unique_fb($data); } /** * 數(shù)組去重方法 */ function array_unique_fb($array2D){ foreach ($array2D as $v){ $v = join(",",$v); //降維,也可以用implode,將一維數(shù)組轉(zhuǎn)換為用逗號(hào)連接的字符串 $temp[] = $v; } $temp = array_unique($temp); //去掉重復(fù)的字符串,也就是重復(fù)的一維數(shù)組 foreach ($temp as $k => $v){ $temp[$k] = explode(",",$v); //再將拆開的數(shù)組重新組裝 } return $temp; } |
|