How to set the charset to UTF-8 for a received htt

2019-09-08 17:10发布

How to set the charset to UTF-8 for a received http variable in PHP?

I have a html form using the POST methode with 1 input field. But when i submit the form and echo the retrieved the contents from the input field via $_POST['input_name'] i get this: KrkiÄ - but i entered (and i need) this: Krkič

So how can i fix this?

I figured it out now. :)

If i want to add the contents to MYSQL then i need to add this:

if(!$mysqli->set_charset("utf8")){
printf("Error loading character set utf8: %s\n",$mysqli->error);
}

If i just need to echo the contents then adding this meta tag

<meta charset="utf-8">

into html head is enough.

3条回答
乱世女痞
2楼-- · 2019-09-08 17:21

There is no global default charset in PHP -- lots of things are encoding-aware, and each needs to be configured independently.

mb_internal_encoding applies only to the multibyte string family of functions, so it has an effect only if you are already using them (you need to do so most of the time that you operate on multibyte text from PHP code).

Other places where an incorrectly set encoding will give you problems include:

  • The source file itself (saved on the disk using which encoding?)
  • The HTTP headers sent to the browser (display the content received as which encoding?)
  • Your database connection (which encoding should be used to interpret your queries? which encoding for the results sent back to you?)

Each of these needs to be addressed independently, and most of the time they also need to agree among themselves.

Therefore, it is not enough to say "I want to display some characters". You also need to show how you are displaying them, where they are coming from and what the advertised encoding is for your HTML.

查看更多
Lonely孤独者°
3楼-- · 2019-09-08 17:21

on top of your php file place this

 header('Content-Type: text/html; charset="UTF-8"');
查看更多
Ridiculous、
4楼-- · 2019-09-08 17:24

you can use:

<meta charset="UTF-8" />
查看更多
登录 后发表回答