0

I have a web, all encode is set to utf-8, but sometimes, i can see this character � and now, i'm having problems because i want to make an ajax request and if this character exist, the response is null.

I really don't understand because for example, in this image, you can see text that come from the same table, showing ok and with this character. I thought that was the encode of the php file, and change it to utf-8, ansi, etc., but it doesn't matter, the result sometimes was worse

could be the browser from where the data is saved to the database? i don't think so but i have to ask

so, how can i deal with this? i even tried using utf8_encode and decode but it was worse i really don't know what else to try hope you can help me thanks
http://uhma.mx/uploads/images/character.png

Castro Roy
  • 7,623
  • 13
  • 63
  • 97
  • Check that your files are encoded with UTF-8 without BOM. Ensure that your PHP is using UTF-8 by doing: `header('Content-Type: text/html; charset=utf-8');`. If the result is coming in from an external source, such as a DB, check the charset/collation there. That's a start. Also check whether the original string includes the proper character, or does it have that gibberish. – Mark Sep 26 '13 at 20:08
  • Are all of the following set to utf-8: php file, html output (by html meta tag or php `header()`, database connection and possibly database collation? Have any of those ever been set to another charset? This could explain why some entries are displayed correctly and others not. – TheWolf Sep 26 '13 at 20:09
  • @Marcel that really should be an answer :-) – Zathrus Writer Sep 26 '13 at 20:11
  • It felt as more of a comment. Answer submitted. – Mark Sep 26 '13 at 20:13
  • I feel cheated. The people that install the web told me that everything was in utf-8, after reading your comments, i ask them for access to the DB, and when i run `show create database dbname` it say it is in `latin1`, now i don't know what to do. if i change the DB charset some data could be lost? or should i change the code charset in `meta` and `header` to latin1? what is the best solution? – Castro Roy Sep 26 '13 at 21:30
  • "Install[ed] the web". Have fun waiting for that installation process to finish. – Mark Sep 26 '13 at 21:40
  • jajajaja...that was funny, ah?, really men, i'm stuck and looking for help – Castro Roy Sep 26 '13 at 21:47

2 Answers2

1

You need to fetch all data that are being displayed in UTF-8, assuming mysql:

  • set encoding on all columns to utf-8
  • execute SET NAMES 'utf-8' and SET CHARSET 'utf-8' as soon as you open DB connecton

If you need to convert charset from database default (usually latin1), there's already great answer for that here on stack overflow.

Additionally send Content-Type header specifying charset:

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

And also add <meta /> tag with charset:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
1

Check that your files are encoded with UTF-8 without BOM. Ensure that your PHP is using UTF-8 by setting:

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

If the result is coming in from an external source, such as a DB, check the charset/collation there. That's a start. Also check whether the original string includes the proper character, or does it have that gibberish.

Mark
  • 1,376
  • 9
  • 16