note 56973 deleted from function.strtr by danbrown
| From: | danbrown@php.net | 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(" ", "À Á Â Ã Ä Å Ą Ă Ç Ć
Č Ď Đ Ð È É Ê Ë Ę Ě Ğ Ì Í Î Ï
İ Ł Ľ Ĺ Ñ Ń Ň Ò Ó Ô Õ Ö Ø Ő
Ŕ Ř Ś Ş Ť Ţ Ù Ú Û Ü Ů
Ű Ý Ź Ż à á â ã ä å ą ă ç ć
č ď đ è é ê ë ę ě ğ ì í î ï ı
ł ľ ĺ ñ ń ň ð ò ó ô õ ö ø ő ŕ
ř ś ş ť ţ ù ú û ü ů ű ý ÿ
ź ż");
$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.