Matt
Matt

Reputation: 3848

How to display utf8 chinese in html with php

I have chinese characters stored in my mysql database in utf-8, but I need to show them on a webpage that has to be output as charset=ISO-8859-1

When rendered in Latin my test string looks like this "dsfsdfsdf åšä¸€ä¸ªæµ‹è¯•"

I have tried using htmlentities in the following ways because I can't tell from the php docs if $encoding refers to the encoding of the input string or desired output string.

$row['admin_comment']=htmlentities( $row['admin_comment'] ,
                        ENT_COMPAT | ENT_HTML401 ,
                        'ISO-8859-1' , 
                        false  );

$row['admin_comment']=htmlentities( $row['admin_comment'] ,
                        ENT_COMPAT | ENT_HTML401 ,
                        'UTF-8' , 
                        false  );

But both have output string unchanged

Upvotes: 0

Views: 547

Answers (3)

Matt
Matt

Reputation: 3848

It turns out you can set an iframe in your page to a different encoding.

Upvotes: 0

Joni
Joni

Reputation: 111269

The htmlentities function does not convert characters into their numeric character entities. For that you can use the mb_encode_numericentity function:

$row['admin_comment'] = mb_encode_numericentity($row['admin_comment'], 
                            array(0xFF, 0xFFFF, 0, 0xFFFF), "UTF-8");

You probably should look into migrating to UTF-8 though.

Upvotes: 0

STT LCU
STT LCU

Reputation: 4330

You cannot output chinese character in the ISO-8859-1 charset. It's simply impossible.

You have 2 possibilities:

  1. stick to UTF-8 (recommended)
  2. pick another chinese-compatible charset (BIG5 If my memory serves me right)

Why your page MUST be rendered as LATIN-1? I find this requirement very strange. My suggestion is to use EVERYWHERE (from DataBase encoding to HTML rendering) the UTF-8 charset. It will save you A LOT of pain in the future.

Upvotes: 3

Related Questions