PHP form submit UTF-8
In my website there is a form with a simple textarea for people to post comments. The problem is that sometimes I receive information in UTF-8 and sometimes in ISO. Is it possible to control that?
Maybe I am doing something wrong, but is it possible that the browser changes开发者_运维知识库 the codification of the data it sends?
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
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.
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
But... check before encoding, if string is already UTF-8. Else you double-encode it.
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.
I solved this problem by changing mbstring.http_input = pass in my php.ini file
You could encode the $_POST data into UTF-8 using PHP's utf8_encode function.
Something like:
$_POST['comments'] = utf8_encode( $_POST['comments'] );
精彩评论