[英]Highlight multiple keywords in search (non-english characters)
您確定要搜索Unicode字符嗎? \\ w僅匹配[a-zA-Z0-9_]。
例如,這匹配波斯數字:
preg_match( "/[^\x{06F0}-\x{06F9}\x]+/u" , '۱۲۳۴۵۶۷۸۹۰' );
來源: http : //php.net/manual/en/function.preg-match.php
只需使用要匹配的字符創建一個字符類。
頁面加載所需的編碼后,最好使用JS來實現:
function paint_keys( str_to_replace , words ){
temp_reg = str_to_replace;
if( /\s/g.test( temp_reg ) ) temp_reg = temp_reg.split( " " ); // Breaking our searching keywords into an array;
else return words.split( temp_reg ).join( "<span class='painted'>" + temp_reg + "</span>" );
for( z = 0 ; z < temp_reg.length ; z++ ){
if( temp_reg[ z ] != "" ) words = words.split( temp_reg[ z ] ).join( "<span class='painted'>" + temp_reg[ z ] + "</span>" );
}
return words;
}
當然,您可以根據需要替換<span class='painted'>" + temp_reg + "</span>
。
如果您只需要使用php來完成它-使用通過explode( "\\s" , $words );
破壞鍵來排列數組的原理explode( "\\s" , $words );
-這只會為您提供一個符號數組,然后循環為該數組中的每個單詞替換字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.