0

I created a CMS in PHP using PDO/MySQL. On each page in the CMS I have the HTML charset entered as UTF-8:

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

I have a couple of <textfield> elements that the user can enter some descriptive text of the item into. This works well for strings that are typed out into the <textarea>, however the client has a tendency to copy and paste from another document, by the looks of it. Every so often I see a � on the page, however, in the database, it's actually a hyphen, or an apostrophe, etc.

I know that it's an encoding issue, but what I'm not sure of is where the problem is. If I'm setting the page charset as UTF-8, should the content not save as UTF-8?

If I change the charset to Latin-1 (ISO 8859-1) the symbol appears correctly. I was under the impression that UTF-8 had a broader character range than Latin-1, which kind of adds to the confusion.

Is there a work around for this in PHP? Can I force a field in MySQL to be a specific charset?

I have checked a few Questions and Answers on SSO without much luck. Most of the answers are "Make sure you're declaring your charset", which I am.

matcartmill
  • 1,573
  • 2
  • 19
  • 38
  • What is the character encoding declared for the database and tables within the database? Utf8 as well? – Cedric Ipkiss Apr 04 '15 at 02:06
  • Oh, good call. I kind of overlooked that. They're set to latin1_swedish_ci, which is the default setting, I believe. Should I be setting them to utf8_general_ci? That's the only option that seems viable in the utf8 section. – matcartmill Apr 04 '15 at 02:08
  • I set the table to `utf8_general` and it seems to be working now. It doesn't fix the older posts that used Latin-1 characters, however it does fix it for new one's. I can copy and paste in old info into a new post and there are no more � symbols. If you'd like to post your comment as an answer, I will go ahead and accept it as the answer. – matcartmill Apr 04 '15 at 02:15
  • Let's see the output of `SELECT col, HEX(col) ...` to see whether the data has been stored correctly, plus let's see `SHOW CREATE TABLE`. – Rick James Apr 04 '15 at 04:53

0 Answers0