您這樣做的方式,您將始終顯示用戶輸入的內容,並且實際上PHP不會與MySQL LIKE
進行相同的寬鬆比較。
這是我寫的一個處理這個問題的函數,包括大部分法語重音字符。
function highlight_substring($string, $substring)
{
if(empty($string) || empty($substring)) return false;
$normal = array('à', 'é', 'è', 'ê', 'ë', 'î', 'ï', 'ô', 'ò', 'ö', 'û', 'ü', 'ù', 'ç');
$flat = array('a', 'e', 'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'u', 'u', 'u', 'c');
$str = mb_strtolower($string);
$str = str_replace($normal, $flat, $str);
$sub = mb_strtolower($substring);
$sub = str_replace($normal, $flat, $sub);
$pos = mb_strpos($str, $sub);
if($pos !== false)
{
$var = mb_substr($string, 0, $pos).'<span class="highlight">'.mb_substr($string, $pos, mb_strlen($substring)).'</span>';
$var .= mb_substr($string,(bcadd(mb_strlen($substring), $pos)));
$string = $var;
}
return $string;
}
隨意調整和改進;)
使用
echo highlight_substring('Allons à l’école !', 'ecole'); // user input 'ecole'
echo highlight_substring('Allons à l’École !', 'ecole'); // user input 'ecole'
echo highlight_substring('Allons à l’école !', 'Ecole'); // user input 'Ecole'
將輸出:
Allons à l’<span class="highlight">école</span> !
Allons à l’<span class="highlight">École</span> !
Allons à l’<span class="highlight">école</span> !
因此,要總結,你想'somefunction(「fooâccéntbar」,「accent」)=「fooéccént bar」'?這是一個很好的問題,我從來沒有處理過這個問題...... –
Wrikken
目前,我只能想到一個帶有正則表達式的'preg_replace_callback',該正則表達式匹配任何在兩側都有字邊界的東西'\ b 。*?\ b',它檢查一個'iconv'到ascii是否會生成您的searchterm,並將其返回爲高亮顯示,如果不是,則按原樣顯示。 – Wrikken