2015-01-13 16 views
0

我想要實現的是 - 我想用preg-replace在建議中突出顯示搜索字符串,但忽略字符,空格或撇號上的變音符號。所以,當我將例如搜索公頃我的搜索建議會是這樣的:如何匹配正則表達式unicode文本忽略字符上的變音符號

  • O」 RA
  • Ó的Ç intighe
  • H'a SOMETHING

我做了大量的研究,但沒有拿出任何代碼呢。我只是有一個想法,我可以用變音符(例如:Á,É...)將字符轉換爲字符和修飾符(A +',E +'),但我不知道如何去做。

+0

可能重複[如何從文本變音符號?](http://stackoverflow.com/questions/1770250/how-to-remove-diacritics-from-text) –

+0

但我不想刪除變音符 - 我只是想用正則表達式「忽略」它。 –

+0

您可以刪除變音符號並使用刪除了所有變音符號的字符串。 PCRE不提供「等效字符」功能。 – nhahtdh

回答

0

我終於找到了在這裏工作的解決方案感謝這個蒂博爾的回答是:Regex to ignore accents? PHP

我的功能凸顯文本忽略變音符號,空格,省略號和破折號:

function highlight($pattern, $string) 
    { 
    $array = str_split($pattern); 

    //add or remove characters to be ignored 
    $pattern=implode('[\s\'\-]*', $array); 

    //list of letters with diacritics 
    $replacements = Array("a" => "[áa]", "e"=>"[ée]", "i"=>"[íi]", "o"=>"[óo]", "u"=>"[úu]", "A" => "[ÁA]", "E"=>"[ÉE]", "I"=>"[ÍI]", "O"=>"[ÓO]", "U"=>"[ÚU]"); 

    $pattern=str_replace(array_keys($replacements), $replacements, $pattern); 

    //instead of <u> you can use <b>, <i> or even <div> or <span> with css class 
    return preg_replace("/(" . $pattern . ")/ui", "<u>\\1</u>", $string); 
    } 
+0

「àÀèÈêÈêÈëËËÎÎïÏôÔùÙüÜçÇ'?只是在談法國的變音符號。 – Toto

+0

只需將這些字符添加到列表中,它應該可以工作。 –

+0

當然,但我只是對你說,你最好改變你的答案,這不是我的問題,我已經知道如何處理。 – Toto

相關問題