Jump to content

utf8_encode not working


bravo14

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 post
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 post
Share on other sites

The text is showing correctly in phpmyadmin in both safari and firefox but in the same browsers the characters are showing as a quesiton mark, not the question mark in a diamond, it is also showing correctly in mysql workbench

Link to post
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 post
Share on other sites

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 post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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