note 56973 deleted from function.strtr by danbrown

From: Date: Thu, 04 Mar 2010 18:21:29 +0000
Subject: note 56973 deleted from function.strtr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166673@lists.php.net to get a copy of this message
Note Submitter: derernst at gmx dot ch ---- 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.

« previous php.notes (#166673) next »