Jump to content

utf8_encode


bravo14
 Share

Recommended Posts

Hi

 

I have a database table containing 'european' names e.g.Roman Čejka

 

To echo this on a page I have used

 

 

<?php echo utf8_encode($gp_row['reserve2']);?>

 

However the Č is still showing as '?', any ideas what I am doing wrong?

Link to comment
Share on other sites

However the Č is still showing as '?', any ideas what I am doing wrong?

 

You didn't read the manual. The function utf8_encode() transcodes a string from ISO 8859-1 to UTF-8. Since your input clearly isn't ISO 8859-1, this function doesn't help you at all.

 

I guess what you actually want is declare the character encoding of the HTML document as UTF-8 so that browsers display it correctly. Put this somewhere on top of your script:

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

In addition to this, you also have to set the encoding of the database connection to UTF-8:

// If you're using PDO, set the character encoding in the DSN string.
$database = new PDO('mysql:host=localhost;dbname=YOURDB;charset=utf8', 'YOURUSER', 'YOURPASSWORD', $options);

// If you're using MySQLi, set the encoding right after the connection
$database->set_charset('utf8');

// If you're still using the old MySQL extension ...
mysql_set_charset('utf8');

Do not use a SET NAMES query. This silently changes the connection encoding without notifying PHP about it. As a result, all escaping functions will continue to use the old encoding and may stop working entirely.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.