note 56973 added to function.strtr

From: Date: Tue, 20 Sep 2005 17:29:40 +0000
Subject: note 56973 added to function.strtr
Groups: php.notes 
Request: Send a blank email to php-notes+get-95487@lists.php.net to get a copy of this message
This works for me to remove accents for some characters of Latin-1, Latin-2 and Turkish in a UTF-8 environment, where the htmlentities-based solutions fail: <?php function remove_accents($string, $german=false) { // Single letters $single_fr = explode(" ", "À Á Â Ã Ä Å &#260; &#258; Ç &#262; &#268; &#270; &#272; Ð È É Ê Ë &#280; &#282; &#286; Ì Í Î Ï &#304; &#321; &#317; &#313; Ñ &#323; &#327; Ò Ó Ô Õ Ö Ø &#336; &#340; &#344; Š &#346; &#350; &#356; &#354; Ù Ú Û Ü &#366; &#368; Ý Ž &#377; &#379; à á â ã ä å &#261; &#259; ç &#263; &#269; &#271; &#273; è é ê ë &#281; &#283; &#287; ì í î ï &#305; &#322; &#318; &#314; ñ &#324; &#328; ð ò ó ô õ ö ø &#337; &#341; &#345; &#347; š &#351; &#357; &#355; ù ú û ü &#367; &#369; ý ÿ ž &#378; &#380;"); $single_to = explode(" ", "A A A A A A A A C C C D D D E E E E E E G I I I I I L L L N N N O O O O O O O R R S S S T T U U U U U U Y Z Z Z a a a a a a a a c c c d d e e e e e e g i i i i i l l l n n n o o o o o o o o r r s s s t t u u u u u u y y z z z"); $single = array(); for ($i=0; $i<count($single_fr); $i++) { $single[$single_fr[$i]] = $single_to[$i]; } // Ligatures $ligatures = array("Æ"=>"Ae", "æ"=>"ae", "Œ"=>"Oe", "œ"=>"oe", "ß"=>"ss"); // German umlauts $umlauts = array("Ä"=>"Ae", "ä"=>"ae", "Ö"=>"Oe", "ö"=>"oe", "Ü"=>"Ue", "ü"=>"ue"); // Replace $replacements = array_merge($single, $ligatures); if ($german) $replacements = array_merge($replacements, $umlauts); $string = strtr($string, $replacements); return $string; } ?> I am sorry as the display of the user notes is broken the character lists shown will not be useable by now, but you see the principle. Speakers of certain languages might want to suggest changes or additions to the character lists. Characters are divided by spaces and explode()d because I am on PHP4, and other splitting methods fail with double byte characters. In PHP5 split() might help, too. The advantage of this approach is that non-latin characters remain untouched. ---- Manual Page -- http://www.php.net/manual/en/function.strtr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56973 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56973&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56973&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95487) next »