I have the following string as a varchar field in a MySQL Database:


When I perform a query to extract the string and then print it to the screen using the PHP print command, I get:


What could be causing this? I've tried commands like str_replace, htmlspecialchars, and addslashes to no avail. How should I store/print this string?

  • Have you specified character encoding? – Adjam May 10 '12 at 4:05
  • @Adjam My apologies, but I'm not sure what you mean. I'm a novice at this. – Austin Mohr May 10 '12 at 4:06
  • You need to specify the character encoding, otherwise the browser will not know how to render many characters. Try adding this line of code in the <head> of your HTML document <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> – Adjam May 10 '12 at 4:15
  • If that doesn't work, give this a go too, let us know if it does/doesn't work <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> – Adjam May 10 '12 at 4:23

Is the string I'm in your question actually copy-n-pasted from the database via adminer/phpmyadmin/mysqlcli/whatever? Or is it retyped?

I suspect that what's stored in your database is not I'm at all, but rather something like I’m with a typographer's apostrophe. Like the other comments have said, you can and should fix the problem by consistently using a single character set across your CMS, database, database server, and webapp. UTF-8 is a good choice.

Also, as mentioned by jobjorn, you can convert strings' character sets between different character sets. This is especially useful when you don't have control over all components (cms, db, server, webapp, etc).

  • You're exactly correct about the typographer's apostrophe. I went in to the field and manually typed the apostrophe from my keyboard and it resolved the issue. How it got that way in the first place is still a mystery, but I'm just happy to have it fixed. Many thanks. – Austin Mohr May 10 '12 at 7:35
  • It seems that the apostrophe key renders as the typographer's apostrophe in MS Word. Since I copied my data from a Word document into a .txt and then uploaded to the database, I suspect this is how the strange apostrophes got there. – Austin Mohr May 10 '12 at 8:11
  • 1
    Word is a common culprit. The resilient solution lies in using consistent charsets throughout the components of your application. – matty May 10 '12 at 8:16

As Adjam said, it is probably related to Character encoding, a hassle for everyone involved. Check what you are using in your database (probably ISO-8859-1 or UTF-8) and use the same (preferably UTF-8) in your document - both with the meta tag, and the actual file. Depending on what editor you use, you may have an option to save your file in a selectable character encoding.

For a quick fix, you could try the php functions utf8_decode() or utf8_encode().


Probably your character encoding has not been specified correctly. You can experiment with the effects of character encoding here.

In the code of the PHP file, between the <head> and </head> tags, try using different character encodings such as:

  • <meta charset='utf-8'>
  • <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  • <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

Some questions:

  • Do any of these suggestions fix the problem?
  • How is the ' rendered in the source of the webpage (Right Click > View Source)
    • is it rendered as or as '?
  • What editor are you using to save/edit your php files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.