Jump to content

utf8_encode not working


Go to solution Solved by Jacques1,

Recommended Posts

Hi guys


I am trying to echo a database record, the record is stored in polish and is stored ok.  When I am echoing the data I am using the utf8_encode($row['headline'])


However some of the characters are showign as ? the charset of the page is set to


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


Is there anything else I am missing or anything else may be causing the problem.



Link to comment
Share on other sites

I think...


Even though everyone is agreeing to use utf-8, that does not mean the font in play has the glyphs you want to show.


What application are you using to verify that the database has the correct characters? What application are you using where you see the replacement character (white question mark in black diamond)?


Do both use the same font?

Link to comment
Share on other sites

What do you use to edit your PHP file?  Check the the files the editor creates are encoded in UTF-8.

Is UTF-8 your default charset with PHP? Check with phpinfo(); the value default_charset

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

Do you use any string functions on the data before echoing it?

Link to comment
Share on other sites

  • Solution

There seems to be a whole lot of misunderstandings.


The function utf8_encode() function expects an ISO 8859-1 encoded string (read the manual!) and transcodes it to UTF-8. I'm fairly sure this is not what you want. If your data already is encoded with UTF-8, the function will fail miserably. And if your data actually is ISO 8859-1, it doesn't make a terrible lot of sense to convert it to UTF-8, because this only increases the data size.


A typical cause for database-related encoding issues is that the connection isn't set to the right encoding. It's not enough to declare the database as UTF-8. You also have to tell MySQL that all outgoing data should be encoded as UTF-8. Otherwise it will use the default encoding which is usually “Latin1” (a variant of ISO 8859-1).


So you need to take care of three encodings:

  • The encoding of the database itself (you seem to have already done that).
  • The encoding of the database connection. This depends on which database extension you use. PDO? MySQLi? The old ext/mysql? For PDO, the encoding is set in the constructor, MySQL has mysqli_set_charset(), and the old extension has mysql_set_charset().
  • Last but not least, you have to declare the HTML document as UTF-8. This should not be done with a meta element but within the Content-Type header: Content-Type: text/html; charset=utf-8
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.

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.


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