php form submit utf8?

2020-02-05 08:20发布

问题:

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?

Maybe I am doing something wrong, but is it possible that the browser changes the codification of the data it sends?

thanks

回答1:

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



回答2:

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"/>


回答3:

but ... check before encoding, if string is already utf8 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



回答4:

I solved this problem by changing mbstring.http_input = pass in my php.ini file



回答5:

Have you testes with multiple different browsers if the proble occurs on all/some of them or only IE?



回答6:

You could encode the $_POST data into UTF-8 using PHP's utf8_encode function.

Something like:

$_POST['comments'] = utf8_encode( $_POST['comments'] );