note 47295 added to function.str-replace

From: Date: Wed, 10 Nov 2004 13:09:03 +0000
Subject: note 47295 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-80343@lists.php.net to get a copy of this message
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ÿöñë!"); ?> ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47295 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47295&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47295&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80343) next »