note 47295 deleted from function.str-replace by bjori

From: Date: Sun, 26 Mar 2006 22:26:40 +0000
Subject: note 47295 deleted from function.str-replace by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106680@lists.php.net to get a copy of this message
Note Submitter: Jennings at trad dot uji dot es ---- Removing accents. It is a pain to list all the accented characters you think you will come across - and you never know when someone is going to use an unexpected 'ÿ' or other such obscure character. In the HTML Entities table the accented characters are described in such a way that the second character (ie the one after after the ampersand) is always the same as the unaccented form of the accented letter (and in the same case). 'È', for example, is '&Eaccute;'. This function takes advantage of that fact and removes all accents. <?php function unaccent($text){ $trans = get_html_translation_table(HTML_ENTITIES); //Get the entities table into an array foreach ($trans as $literal =>$entity){ //Create two arrays, for accented and unaccented forms if (ord($literal)>=192){ //Don't contemplate other characters such as fractions, quotes etc $replace[]=substr($entity,1,1); //Get 'E' from string '&Eaccute' etc. $search[]=$literal;}} //Get accented form of the letter return str_replace($search, $replace, $text);} echo unaccent("Hêllò Èvérÿöñë!"); ?>

« previous php.notes (#106680) next »