PHP & Unicode

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello all,

I have a simple question on PHP and the way it handles strings.

Let's say I have a database and a php script that communicates with the
database. The database has some kind of character encoding - let's say
UTF-8, UTF-16, or something different.

When I select some string from the database, for example:

$res = mysql_query("SELECT movie_name FROM my_table");
$row = mysql_fetch_row($res);
echo $row[0];

the database will return the movie_name field in a correctly-encoded

My question is, how do you tell the PHP interpreter what encoding to
use when displaying the text that the mysql queries return? In other
words, will the $row[0] be displayed correctly regardless the database
encoding, provided the database encoding and the HTML <meta> tags are
the same, or do I have to set the PHP encoding in some config file?
What character representation does PHP use when working with strings?

Best Regards,

Re: PHP & Unicode

Not sure since I have some problems regarding encoding, but it should be  
  enough to have the html tag matching your db's encoding. Php shouldn't  

JD wrote:
Quoted text here. Click to load it

Re: PHP & Unicode wrote:


Quoted text here. Click to load it


Quoted text here. Click to load it

No. First off you'll need to use a character repertoire that makes sense  
on the Web. utf-8 makes sense, utf-16 does not. So if your database uses  
utf-16, you'll need to transliterate to utf-8 before serving.[*]

In addition you need to ensure that the user-agent (a browser for  
example) is informed correctly of which character repertoire applies.  
(Unless you want to rely on chance this is *always* a requirement, with  
any character repertoire. Not just when you work with utf-8.) You do so  
by having your server accompany the document with an appropriate  
Content-Type header. For example, if it's a utf-8 encoded HTML file,  
your server must say Content-Type: text/html; charset=utf-8. (Whether  
the file name extension is ".php" or ".html" is irrelevant)

An alternative to configuring the server to do so is to have PHP  
generate the Content-Type header:

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

Contrary to popular belief, a META HTTP-EQUIV is *not* a realiable  

- I'm not entirely sure what you mean with "displaying". PHP doesn't  
display. Nor does a Web server. It is the *browser*'s job to "display"  
(whether visually or otherwise).
- all this assumes what you're trying to do is meant for the Web. An  
intranet situation may have different requirements and possibilities.

[*] How exactly to do transliteration in PHP I can't tell you. I'm sure  
it can be found in the documentation. It might also be that your  
database allows you to request output in a specific character  
repertoire. If so, that route might be more efficient.

Sander Tekelenburg, <

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"

Site Timeline