您如何知道用户在浏览器中输入的编码? [英] How do you know what encoding the user is inputing into the browser?
问题描述
我阅读了 Joel关于角色集的文章,所以我正在采取他的建议在我的网页和我的数据库中使用UTF-8。我不明白的是用户输入怎么办正如乔尔所说:不用知道使用什么编码就没有意义。但是如何知道用户输入字符串使用什么编码?如果我有
I read Joel's article about character sets and so I'm taking his advice to use UTF-8 on my web page and in my database. What I can't understand is what to do with user input. As Joel says, "It does not make sense to have a string without knowing what encoding it uses." But how do I know what encoding the user input string uses? If I have
< input type =textname =atextfield>
在我的页面上,我如何知道从用户那里得到什么编码?如果用户输入一些特殊的ASCII符号,如♣或™?有没有办法可以检测到用户输入给了我一些无法识别的UTF-8?有没有一些如何处理这种事情的标准?
on my page, how do I know what encoding I'm getting from the user? What if the user puts in some special ASCII symbol, like ♣ or ™ or something? Is there some way I can detect that user input gave me something unrecognized in UTF-8? Is there some standard for how to handle this sort of thing?
推荐答案
如果您的网页使用UTF-8,浏览器将转换为UTF-8。所以,即使特殊字符是ASCII,它将以UTF-8的形式提交。
If your web-page using UTF-8, browser will convert to UTF-8 for you. So, even the special characters are in ASCII it will submit as UTF-8.
然而,您从不知道用户将页面编码切换回ISO的痒手-8859 - *。
However, you never know itchy hand from an user that switch back the page encoding to ISO-8859-*.
您可以在 mb_detect_encoding ,但不是100%防弹。
You can make use on mb_detect_encoding, but is not 100% bullet-proof.
/* Detect character encoding with current detect_order */
echo mb_detect_encoding($str);
/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
echo mb_detect_encoding($str, "auto");
/* Specify encoding_list character encoding by comma separated list */
echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");
/* Use array to specify encoding_list */
$ary[] = "ASCII";
$ary[] = "JIS";
$ary[] = "EUC-JP";
echo mb_detect_encoding($str, $ary);
这篇关于您如何知道用户在浏览器中输入的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!