468,525 Members | 2,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

PHP & Unicode

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
way.

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,
Slavi

Oct 24 '05 #1
2 2239
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
care.

JD

sl***********@gmail.com wrote:
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
way.

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,
Slavi

Oct 24 '05 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
sl***********@gmail.com wrote:

[...]
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.
[...]
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


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
alternative.
Notes:
- 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, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
Oct 24 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Edward K. Ream | last post: by
27 posts views Thread by EU citizen | last post: by
19 posts views Thread by Philipp Lenssen | last post: by
4 posts views Thread by webdev | last post: by
7 posts views Thread by Robert | last post: by
4 posts views Thread by Tom Fields | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.