johan smohan
johan smohan

Reputation: 13

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

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.

Upvotes: 0

Views: 3970

Answers (3)

Jon
Jon

Reputation: 437366

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.

Upvotes: 3

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

on top of your php file place this

 header('Content-Type: text/html; charset="UTF-8"');

Upvotes: 1

user1946471
user1946471

Reputation:

you can use:

<meta charset="UTF-8" />

Upvotes: 1

Related Questions