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