homepage Welcome to WebmasterWorld Guest from 54.145.183.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Howto Enable UTF-8 in PHP?
HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 4:47 pm on Jan 21, 2008 (gmt 0)

Hi All,

I've enabled UT8 in the MySQL config, PHP config and Apache config.

I can view the UT8 characters correctly in phpMyAdmin. The data is stored correctly in the MySQL db.

However, when I call the data on a PHP page, PHP displays a? instead of the characters from the db.

I've enabled UTf8 in the php.ini
default_charset = "utf-8"

I've also called the header in the PHP page with the UTF8:
header('content-type: text/html; charset: utf-8');

Is there something that I've missed? What do I need to do to get my PHP pages to display the character set correctly?

Note: phpMyAdmin displays UTF8 correctly from the db.

Thanks

 

gosman

10+ Year Member



 
Msg#: 3553642 posted 5:28 pm on Jan 21, 2008 (gmt 0)

Hi HoboTraveler.

I had the same trouble. Try this

<!-- xml version="1.0" encoding="UTF-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php echo $utf8variable;?>
<body>
<html>

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 6:41 pm on Jan 21, 2008 (gmt 0)

Hello,

That did not work.

Anyone have suggestions?

TIA

PHP_Chimp

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3553642 posted 6:51 pm on Jan 21, 2008 (gmt 0)

Have you tried with more than 1 browser, to check if this is a browser issue, rather than a php/html issue?

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 7:11 pm on Jan 21, 2008 (gmt 0)

Yes, I tested the script on Internet Explorer, Firefox an Opera. The? appeared on all three browsers.

This seems like an issue with PHP. hmm.. I guess if that was the case phpMyAdmin would display the? too but phpMyAdmin has no issues.

TIA

[edited by: HoboTraveler at 7:13 pm (utc) on Jan. 21, 2008]

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3553642 posted 7:17 pm on Jan 21, 2008 (gmt 0)

are you setting content-type properly in the html document?

ksks

5+ Year Member



 
Msg#: 3553642 posted 9:49 pm on Jan 21, 2008 (gmt 0)

Try
<?php
mb_internal_encoding("UTF-8");
?>

first line of php code...I had to do this when trying a Bulgarian -> English translator type project.

mipapage

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 11:19 pm on Jan 21, 2008 (gmt 0)

In firefox, right click and view page info. Check for the "Encoding". What does it say?

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 5:19 am on Jan 22, 2008 (gmt 0)

@ksks,

The mb_internal_encoding did not work.

@mipapage,

Firefox > Character Encoding > is on Unicode(UTF-8)

Internet Explorer > is on Unicode(UTF-8)

mipapage

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 6:58 am on Jan 22, 2008 (gmt 0)

Okay HoboTraveler,

You know that the page is in utf-8, so my guess is that you are double utf-8 encoding somewhere. Sounds strange, but I seem to recall having done this myself somewhere. Besides the two things you outlined in your first message, is there anything else you are trying in the process to get these characters to utf-8?

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 7:52 am on Jan 22, 2008 (gmt 0)

Hello,

I've listed below the contents of my AMP config files. These are the only config areas where I've enabled UTF8.

php.ini contents
default_charset = "utf-8"

my.cnf contents
[client]
default-character-set=utf8

[mysqld]
character-set-server=utf8
default-character-set=utf8
default-collation=utf8_unicode_ci
character-set-client = utf8

httpd.conf contents
AddDefaultCharset UTF-8

and the php script itself has this in the first line:
header('content-type: text/html; charset: utf-8');

TIA

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 9:26 am on Jan 22, 2008 (gmt 0)

since you have covered all aspects of headers and encoding...

perhpas your data were saved in db in utf-8 encoding.... have you tried to utf8_decode() your data to screen?

long shot here, but there is nothing else i can think of

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 10:30 am on Jan 22, 2008 (gmt 0)

Hello,

The utf8 decode did not work.

I've listed two records from the table. These are greek characters.

&#913;&#928;&#927;&#923;&#937;&#925;&#906;&#913;k
&#931;&#906;&#934;&#925;&#927;&#931;

Maybe someone could insert these in a UTF8 MySQL and call them from a PHP script.

TIA

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 10:49 am on Jan 22, 2008 (gmt 0)

first line read APOLONIA and second line reads SIFNOS (in greek characters), all capitalized

i am curious... did this problem appeared during the transfer of a database export to another db?

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 11:31 am on Jan 22, 2008 (gmt 0)

@omoutop,

You are correct. The first line is APOLONIA and second is SIFNOS (greek).

This issue did not occur between databases. I imported the characters correctly into MySQL and have not been able to read them from my PHP scripts since.

This forum encoded the Greek characters that I pasted earlier.

How did you insert the characters into your db? Did you insert the string that I pasted? In my db, the names are inserted in the non-encoded format. That is, APOLONIA and SIFNOS in greek.

Were you able to store the non-encoded names into the db and then read them correctly through your PHP script? If so, I would appreciate if you could share your PHP script.

Thank You

Romeo

10+ Year Member



 
Msg#: 3553642 posted 11:32 am on Jan 22, 2008 (gmt 0)

Besides the definitions already discussed, the connection between the client and server may use own connection-related character set and collation system variables.

So you may try to add a:
[mysqld]
init-connect='SET NAMES utf8'

to set this as a default for all connections, or start with these queries after your specific script connects to the database before sending other queries:
SET NAMES utf8;
SET CHARACTER_SET utf8;

Kind regards,
R.

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 11:43 am on Jan 22, 2008 (gmt 0)

nope, i didnt insert them into db, i just pste them into a iso-8859-7 (greek) and a utf-8 encoding page to read them.
They read correclty in both pages.

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 12:01 pm on Jan 22, 2008 (gmt 0)

@Romeo,

Adding the init-connect did not work.

I am pasting below the output for show variables. Maybe there is something there I may have missed.

mysql> show variables like 'c%';
+--------------------------+-------------------------------------------------------------------+
¦ Variable_name ¦ Value ¦
+--------------------------+-------------------------------------------------------------------+
¦ character_set_client ¦ utf8 ¦
¦ character_set_connection ¦ utf8 ¦
¦ character_set_database ¦ utf8 ¦
¦ character_set_filesystem ¦ binary ¦
¦ character_set_results ¦ utf8 ¦
¦ character_set_server ¦ utf8 ¦
¦ character_set_system ¦ utf8 ¦
¦ character_sets_dir ¦ /usr/local/mysql-standard-5.0.27-linux-i686/share/mysql/charsets/ ¦
¦ collation_connection ¦ utf8_general_ci ¦
¦ collation_database ¦ utf8_unicode_ci ¦
¦ collation_server ¦ utf8_unicode_ci ¦
¦ completion_type ¦ 0 ¦
¦ concurrent_insert ¦ 1 ¦
¦ connect_timeout ¦ 5 ¦
+--------------------------+-------------------------------------------------------------------+

TIA

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 12:38 pm on Jan 22, 2008 (gmt 0)

It worked!

I was able to read the UTF8 characters correctly from the db!

What happened was, I was calling an includes file that contains the database connect info. The includes file connected to multiple databases, some latin and some UTF8.

I removed the includes file and tested by just calling the UTF8 database. The data displayed correctly.

How do I get UTF8 to work with the includes file? Is there a way to specify the UTF8 in the mysql_connect or mysql_select_db function? I guess that would selectively force UTF8 on the database that uses it?

TIA

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 12:41 pm on Jan 22, 2008 (gmt 0)

try somethig like:
mysql_query(SET NAMES utf8);
after mysql connection.

HoboTraveler

5+ Year Member



 
Msg#: 3553642 posted 12:43 pm on Jan 22, 2008 (gmt 0)

Update:

I think I got it to work with multiple databases!

I've inserted the line:

mysql_query('SET NAMES utf8');

just after the mysql_connect() and it seems to work. I though this line was not needed since UTF8 is forced in the MySQL config file..?

Thank You all for your help.

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 12:50 pm on Jan 22, 2008 (gmt 0)

Nice!
good job and well done :)

mipapage

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 1:23 pm on Jan 22, 2008 (gmt 0)

I've listed two records from the table. These are greek characters.

&#913;&#928;&#927;&#923;&#937;&#925;&#906;&#913;k
&#931;&#906;&#934;&#925;&#927;&#931;

Is that what you see in phpmyadmin in the textbox/area for that data? If so, Ive seen this before but cannot recall exactly what we did to sort it out.

We had a mysql db storing Cyrillic characters as entities, as you do, but you want mysql to store the utf-8 characters. What we did here was to work over the DB so that it stored the actual utf-8 characters, not the entities.

I'll have to poke around but I remember using some functions that I found on php.net, including utf8dec, uft8html2utf8 and some others.

mipapage

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3553642 posted 1:24 pm on Jan 22, 2008 (gmt 0)

Oops, looks like I was too late. Glad you got it sorted out :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved