13

I have these Chinese characters:

汉字/漢字''test

If I do

echo utf8_encode($chinesevar);

it displays

??/??''test

Or even if I just do a simple

echo $chinesevar

it still displays some weird characters...

So how am I going to display these Chinese characters without using the <meta> tag with the UTF-8 thingy .. or the ini_set UTF-8 thing or even the header() thing with UTF-8?

Heidelbergensis
  • 475
  • 2
  • 5
  • 18
sasori
  • 5,249
  • 16
  • 86
  • 138
  • 2
    where are you displaying this? in a web page? is the webpage set to utf-8 as well? you have to maintain the same charset throughout the entire rendering pipeline. – Marc B Nov 21 '12 at 15:17
  • 1
    [Works fine for me](http://codepad.org/aWn4l6Gx). What is your setup? – Naftali Nov 21 '12 at 15:18
  • Which editor are you using to make the .php ? – Weacked Nov 21 '12 at 15:19
  • i checked the main layout file, it has `` ..i guess the whole portal isset to utf-8 ...but still am getting weird characters in my example..i also tried crawling the php manual, i can't find any functions that can help me achieve my objective – sasori Nov 21 '12 at 15:21
  • am using netbeans at work...right now at home, am using rapidphp2011, and trying to test any possible solution that i can find so that tomorrow i can fix this annoying issue – sasori Nov 21 '12 at 15:26
  • 1
    @sasori It sounds like the characters are not stored in UTF-8 then, make sure your source file is saved as UTF-8. Also make sure that the `` tag is the very first child of the `` – DaveRandom Nov 21 '12 at 15:28

5 Answers5

16

Simple:

  1. save your source code in UTF-8
  2. output an HTTP header to specify to your browser that it should interpret the page using UTF-8:

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

Done.

utf8_encode is for converting Latin-1 encoded strings to UTF-8. You don't need it.

For more details, see Handling Unicode Front To Back In A Web App.

deceze
  • 510,633
  • 85
  • 743
  • 889
3

$chinesevarOK = mb_convert_encoding($chinesevar, 'HTML-ENTITIES', 'UTF-8');

karrtojal
  • 796
  • 7
  • 16
2

Look that your file is in UTF8 without BOM and that your webserver deliver your site in UTF-8

HTML:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

in PHP:

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

And if you work with a database look that your database is in UTF-8 if you read the text from your database.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

Perhaps take a look at the following solutions:

  1. Your database, table and field COLLATE should be utf8_unicode_ci
  2. Check if your records are showing the correct characters within the database...
  3. Set your html to utf8

  4. Add the following line to your php after connecting to the database

    mysqli_set_charset($con,"utf8");

http://www.w3schools.com/php/func_mysqli_set_charset.asp

Stephan
  • 91
  • 1
  • 5
0

save your source code in UTF-8 No BOM

unigg
  • 466
  • 3
  • 8