iconv返回奇怪的结果 [英] iconv returns strange results

查看:271
本文介绍了iconv返回奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种使用PHP中创建帐户的自动脚本中的特殊字符来解决问题的方法.由于在电子邮件地址和其他地方不需要使用特殊字符,因此我试图摆脱它们,但是由于必须将用户名正确显示给其他用户,因此我无法在将它们输入脚本之前将其删除.

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.

示例: 约尔根·戈茨(JörgenGötz)应该获得电子邮件地址jorgen.gotz@domain.com,但是在用户数据库中,他的名字仍然应该是约尔根(Jörgen)和他的姓氏(Götz).我希望我不会不清楚我想要实现什么.

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.

我一直在尝试iconv(),但是遇到了一些麻烦.参见下面的代码.

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 undGötz';

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

setlocale(LC_ALL,'en_GB');

setlocale(LC_ALL, 'en_GB');

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

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

上面的代码应该返回

Weiss,Goldmann,Gobel,Weiss,Gothe,Goethe和Gotz

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

但相反,它给了我

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

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

我不明白那里的报价.

Chrome和IE都给我相同的结果,并且该页面使用的是charset ="utf-8".

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

在使用iconv()之前,我尝试将strtr()与不需要的"数组一起使用.字符,但我不喜欢每次来回转换字符串时都必须设置特殊字符数组的解决方案.

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?

推荐答案

TRANSLIT尝试查找看起来与请求的字符相似的字符. 由于字母ö不在ascii中,因此将其更改为成对的umlat和基本字母.

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.

这篇关于iconv返回奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆