[ACCEPTED]-UTF-8 characters don't display correctly-multibyte

Accepted answer
Score: 16

What's the encoding of your file? It should 20 be UTF8 too. What's the default charset 19 of your http server? It should be UTF-8 18 as well.

Encoding only works if:

  • the file is encoded correctly
  • the server tells what's the encoding of the delivered file.

When working 17 with databases, you also have to set the 16 right encoding for your DB fields and the way 15 the MySQL client communicates with the server 14 (see mysql_set_charset()). Fields only are not enough because 13 your MySQL client (in this case, PHP) could 12 be set to ISO by default and reinterprets 11 the data. So you end up with UTF8 DB -> ISO 10 client -> injected into UTF8 PHP script. No 9 wonder why it's messed up at the end :-)

How 8 to serve the file with the right charset?

header('Content-type: text/html; charset=utf-8') is 7 one solution

.htaccess file containing AddDefaultCharset UTF-8 is 6 another one

HTML meta content-type might 5 work too but it's always better to send 4 this information using HTTP headers.

PS: you 3 also have to use mb_strlen() because strlen() on UTF8 strings 2 will probably report more than the real 1 length.

Score: 4

If you're going to send a mix of data and 2 don't want to specify utf-8 using a php 1 header, you can add this html to your page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
Score: 3

I suppose, your code is in windows-1251 encoding since 2 it is Russian :) convert your string to 1 utf-8:

$str = iconv('windows-1251', 'utf-8', $str);
Score: 2

If your database is UTF-8, it's ok for mysql.

For 2 your echo, if you do it in a web site, put 1 this in the top page:

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

Just add this line at the beginning, after 1 the connection with server:

Score: 0

if you are just using php echo with no html 2 headers etc., this worked great for me.

$connect 1 = mysqli_connect($host_name, $user_name, $password, $database); mysqli_set_charset($connect,"utf8");

More Related questions