php php表单提交utf8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4902062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php form submit utf8?
提问by Raul Lea?o Martinet
In my website there is a form with a simple textarea for people to post comments. The problem is that sometimes I receive info in uft8 and sometimes in iso. Is it possible to control that?
在我的网站上有一个带有简单文本区域的表单,供人们发表评论。问题是有时我在 uft8 中接收信息,有时在 iso 中接收信息。有可能控制吗?
Maybe I am doing something wrong, but is it possible that the browser changes the codification of the data it sends?
也许我做错了什么,但是浏览器有没有可能改变它发送的数据的编码?
thanks
谢谢
回答by Jeremy B.
If you want to be sure of what character set you are accepting, set it in your form
如果您想确定您接受的字符集,请在您的表单中进行设置
<form method="post" action="/your/url/" accept-charset="UTF-8">
</form>
You can see all the acceptable character sets here: Character Sets
您可以在此处查看所有可接受的字符集:字符集
回答by ayush
you can always force UTF-8. Then you can send, receive, and store data in UTF-8 ad cover most human languages without having to change character set.
您始终可以强制使用 UTF-8。然后,您可以发送、接收和存储 UTF-8 格式的数据,并且无需更改字符集即可覆盖大多数人类语言。
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
回答by user4006575
but ... check before encoding, if string is already utf8 else you double-encode it
但是 ... 在编码之前检查,如果字符串已经是 utf8 否则你对它进行双重编码
function str_to_utf8 ($string) {
if (mb_detect_encoding($string, 'UTF-8', true) === false) {
$string = utf8_encode($string);
}
return $str;
}
or use
或使用
$string = utf8_encode(utf8_decode($string));
so you do not double-encode a string
所以你不会对字符串进行双重编码
回答by ujjwal singh
I solved this problem by changing mbstring.http_input = passin my php.ini file
我通过在我的 php.ini 文件中更改mbstring.http_input = pass解决了这个问题
回答by To Ka
Have you testes with multiple different browsers if the proble occurs on all/some of them or only IE?
如果问题出现在所有/部分浏览器或仅 IE 上,您是否使用多个不同的浏览器进行测试?
回答by voidstate
You could encode the $_POST data into UTF-8 using PHP's utf8_encodefunction.
您可以使用 PHP 的utf8_encode函数将 $_POST 数据编码为 UTF-8 。
Something like:
就像是:
$_POST['comments'] = utf8_encode( $_POST['comments'] );