开发者

PHP: Problem converting characters with strtr

开发者 https://www.devze.com 2023-01-14 17:54 出处:网络
I\'ve got the following code from php.net: $GLOBALS[\'normalizeChars\'] = array( \'Š\'=>\'S\', \'š\'=>\'s\', \'Ð\'=>\'Dj\',\'Ž\'=>\'Z\', \'ž\'=>\'z\', \'À\'=>\'A\', \'Á\'=>

I've got the following code from php.net:

$GLOBALS['normalizeChars'] = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'开发者_如何转开发=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f');
$string = strtr($string, $GLOBALS['normalizeChars']);

The string "åäö" should certainly give me "aao", but instead I get "aaa". This is getting really frustrating as I've struggled with it for hours - I mean, it feels like there's not much that can be wrong.

I've tried both setlocale(LC_CTYPE, 'en_US.utf8') and setlocale(LC_CTYPE, 'sv_SE.utf8') and even tried to remove the code and use str_replace('ö', 'o', $string) as well, but nothing is working.

What can possibly be wrong?


Okay, problem solved - it turned out that the file wasn't saved as UTF-8. Works like a charm now, so this was pretty embarrassing.

Thank you guys!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号