iconv returns strange results

微笑、不失礼 提交于 2019-12-24 01:53:36

问题


I'm working on a way to solve the problem with special characters in an automated script for creating accounts in PHP. Since special characters are unwanted in email addresses and other places I'm trying to get rid of them, but I can't remove them before feeding them to the script since the users name has to be displayed properly to other users.

Example: Jörgen Götz should get the email address jorgen.gotz@domain.com but in the user database his first name should still be Jörgen and his last name Götz. I hope I'm not to unclear about what I want to achieve.

I've been experimenting with iconv() but I'm having some trouble with it. See code below.

$utf8_sentence = 'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz';

setlocale(LC_ALL, 'en_GB');

echo $trans_sentence = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_sentence);

The code above should return

Weiss, Goldmann, Gobel, Weiss, Gothe, Goethe und Gotz

but instead it gives me

Weiss, Goldmann, G"obel, Weiss, G"othe, Goethe und G"otz

I can't understand what the quotations are doing there.

Both Chrome and IE gives me the same result and the page is using charset="utf-8".

Before using iconv() I tried using strtr() together with an array of "unwanted" characters but I don't like the sollution of having to set an array of special characters everytime I need to convert strings back and forth.

Can anyone offer an explanation or sollution?


回答1:


TRANSLIT tries to find characters that look similar to the requested character. Since the letter ö is not in ascii it is changing it to a pair for the umlat and the basic letter.




回答2:


Try adding this to your system (terminal in Ubuntu):

sudo locale-gen de_DE.UTF-8

Then changing the locale your php script:

setlocale(LC_ALL, 'de_DE.UTF-8');

Edit (Windows setup)

In Windows Server, you have to install the German Language Pack and change above to:

setlocale(LC_ALL, 'germany');


来源:https://stackoverflow.com/questions/11915040/iconv-returns-strange-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!