Re: case and accent - insensitive regular expression?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jul 14, 2008 at 1:35 PM, Giulio Mastrosanti<giulio@xxxxxxxxxxxxx> wrote:>>>> Brilliant !!!>> so you replace every occurence of every accent variation with all the accent> variations...>> OK, that's it!>> only some more doubts ( regex are still an headhache for me... )>> preg_replace('/[iìíîïĩīĭįı]/iu',...  -- what's the meaning of iu after the> match string?
This page explains them both.http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php
> preg_replace('/[aàáâãäåǻāăą](?!e)/iu',... whats (?!e)  for? -- every> occurence of aàáâãäåǻāăą NOT followed by e?
Yes. It matches any character based on the latin 'a' that is notfollowed by an 'e'. It keeps the pattern from matching the 'a' when itimmediately precedes an 'e' for the character 'ae' for words likethese:http://en.wikipedia.org/wiki/List_of_words_that_may_be_spelled_with_a_ligature(However, that may cause problems with words that have other variantsof 'ae' in them. I'll leave that to you to resolve.)http://us.php.net/manual/en/regexp.reference.php


> Many thanks again for your effort,>> I'm definitely on the good way>>      Giulio>>>>>> I was intrigued by your example, so I played around with it some more>> this morning. My own quick web search yielded a lot of results for>> highlighting search terms, but none that I found did what you're>> after. (I admit I didn't look very deep.) I was up to something like>> this before your reply came in. It's still by no means complete. It>> even handles simple English plurals (words ending in 's' or 'es'), but>> not variations that require changing the word base (like 'daisy' to>> 'daisies').>>>> <?php>> function highlight_search_terms($phrase, $string) {>>   $non_letter_chars = '/[^\pL]/iu';>>   $words = preg_split($non_letter_chars, $phrase);>>>>   $search_words = array();>>   foreach ($words as $word) {>>       if (strlen($word) > 2 && !preg_match($non_letter_chars, $word)) {>>           $search_words[] = $word;>>       }>>   }>>>>   $search_words = array_unique($search_words);>>>>   foreach ($search_words as $word) {>>       $search = preg_quote($word);>>>>       /* repeat for each possible accented character */>>       $search = preg_replace('/(ae|æ|ǽ)/iu', '(ae|æ|ǽ)', $search);>>       $search = preg_replace('/(oe|œ)/iu', '(oe|œ)', $search);>>       $search = preg_replace('/[aàáâãäåǻāăą](?!e)/iu',>> '[aàáâãäåǻāăą]', $search);>>       $search = preg_replace('/[cçćĉċč]/iu', '[cçćĉċč]', $search);>>       $search = preg_replace('/[dďđ]/iu', '[dďđ]', $search);>>       $search = preg_replace('/(?<![ao])[eèéêëēĕėęě]/iu',>> '[eèéêëēĕėęě]', $search);>>       $search = preg_replace('/[gĝğġģ]/iu', '[gĝğġģ]', $search);>>       $search = preg_replace('/[hĥħ]/iu', '[hĥħ]', $search);>>       $search = preg_replace('/[iìíîïĩīĭįı]/iu', '[iìíîïĩīĭįı]', $search);>>       $search = preg_replace('/[jĵ]/iu', '[jĵ]', $search);>>       $search = preg_replace('/[kķĸ]/iu', '[kķĸ]', $search);>>       $search = preg_replace('/[lĺļľŀł]/iu', '[lĺļľŀł]', $search);>>       $search = preg_replace('/[nñńņňʼnŋ]/iu', '[nñńņňʼnŋ]', $search);>>       $search = preg_replace('/[oòóôõöōŏőǿơ](?!e)/iu',>> '[oòóôõöōŏőǿơ]', $search);>>       $search = preg_replace('/[rŕŗř]/iu', '[rŕŗř]', $search);>>       $search = preg_replace('/[sśŝşš]/iu', '[sśŝşš]', $search);>>       $search = preg_replace('/[tţťŧ]/iu', '[tţťŧ]', $search);>>       $search = preg_replace('/[uùúûüũūŭůűųǔǖǘǚǜ]/iu',>> '[uùúûüũūŭůűųǔǖǘǚǜ]', $search);>>       $search = preg_replace('/[wŵ]/iu', '[wŵ]', $search);>>       $search = preg_replace('/[yýÿŷ]/iu', '[yýÿŷ]', $search);>>       $search = preg_replace('/[zźżž]/iu', '[zźżž]', $search);>>>>>>       $string = preg_replace('/\b' . $search . '(e?s)?\b/iu', '<span>> class="keysearch">$0</span>', $string);>>   }>>>>   return $string;>>>> }>> ?>>>>> I still can't help feeling there must be some better way, though.>>>>>>>> well, i think I'm on the good way now, unfortunately I have some other>>> urgent work and can't try it immediately, but I'll let you know    :)>>>>>> thank you!>>>>>>   Giulio>>>>>> Andrew>>>>>>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux