Replacing smart quotes, em-dashes, and ellipses with MySQL or PHP

Alternate title: "Help!  My Quotes Appear as Question Marks or Other Strange Characters!"

The "Smart quotes" feature in Microsoft Office transforms straight quotes into curly quotes.  It also transforms hyphens into em-dashes and three periods into ellipses.  While one might think, "How lovely!  My document looks almost as if I'm educated!" readers of said document may not.  Microsoft, in its infinite wisdom, decided to assign special characters such as the ones we just mentioned to a range of codes above 128.  Problem: these codes aren't compatible with other character sets such as ISO-8859-1 or UTF-8, resulting in frustrating issues with non-Microsoft systems.

Keep reading for some PHP and MySQL code to help out with this issue.

Our introduction to this was in a situation where we had people using many different systems submitting articles to one of our programs.  We decided that we wanted all our articles to use straight quotes, hyphens, and periods.  This was partly for consistency, and partly because these characters are common to many character sets and won't cause incompatibilities.  Should your requirements be different, it should be trivial to modify the code below to fit your specific situation.

Here are some MySQL and PHP techniques for replacing all instances of smart quotes, plus the en dash, em dash, and ellipsis with straight quotes, one or two dashes, or three dots.  This code should operate with both the Windows-1252 charset, and also UTF-8, an extended character set that is in many situations the "best" character set to use for email and websites.

# FIRST, REPLACE UTF-8 characters.
UPDATE `t` SET `c` = REPLACE(`c`, 0xE28098, "'");
UPDATE `t` SET `c` = REPLACE(`c`, 0xE28099, "'");
UPDATE `t` SET `c` = REPLACE(`c`, 0xE2809C, '"');
UPDATE `t` SET `c` = REPLACE(`c`, 0xE2809D, '"');
UPDATE `t` SET `c` = REPLACE(`c`, 0xE28093, '-');
UPDATE `t` SET `c` = REPLACE(`c`, 0xE28094, '--');
UPDATE `t` SET `c` = REPLACE(`c`, 0xE280A6, '...');
# NEXT, REPLACE their Windows-1252 equivalents.
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(145), "'");
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(146), "'");
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(147), '"');
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(148), '"');
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(150), '-');
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(151), '--');
UPDATE `t` SET `c` = REPLACE(`c`, CHAR(133), '...');
// First, replace UTF-8 characters.
$text = str_replace(
 array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
 array("'", "'", '"', '"', '-', '--', '...'),
// Next, replace their Windows-1252 equivalents.
 $text = str_replace(
 array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
 array("'", "'", '"', '"', '-', '--', '...'),
Additionally, here's a table of character codes that you may find useful:
Windows-1252 characters not present in ISO-8859-1

Further reading from Wikipedia:

ISO/IEC 8859-1


Maybe you need a greater range of characters than is available in ISO/IEC 8859-1?  You should use UTF-8 instead:
PHP UTF-8 Cheatsheet
  1. July 17th, 2009 at 14:01 | #1

    I spent about 2 hours trying to figure this out. You're awesome Mango!

  2. Nirav Shah
    July 17th, 2009 at 14:36 | #2

    YOU ROCK Mango!

  3. September 28th, 2009 at 23:08 | #3

    Thanks a LOT dude!!!! I was about to give up on trying to replace ellipses with triple dots till I came across your post. You be a life saver 🙂

    - Ananth

  4. November 29th, 2009 at 16:05 | #4

    very useful - and easily extended to include \x99 and other goofiness. Many thanks.

  5. Jeremy
    December 1st, 2009 at 16:44 | #5

    You are a god among men. I've spent all day trying to fix these dumb smart quotes and nowhere else on the web could I find your bit where you replace the following characters. This solved it. I owe you everything.

    "\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"

  6. December 6th, 2009 at 18:46 | #6

    You guys be careful - you're boosting my already-inflated ego! 🙂

    I am glad to hear this is helping.

  7. February 23rd, 2010 at 16:59 | #7

    Dude, thanks. I was about to tear out one of my three remaining hairs. This was simple, rocks and lets me get on with life.

  8. February 23rd, 2010 at 17:13 | #8

    If Microsoft was preventing you from getting on with your life I am very glad to have been able to help with that!

  9. February 23rd, 2010 at 17:14 | #9

    Welll... I was referring to my programming life... 🙂

  10. theJoker
    August 18th, 2010 at 08:44 | #10

    Thanks for this, I spent a long, long time trying to convert those stupid windows curly quotes. I really appreciate it.

  11. cykan
    September 15th, 2010 at 07:30 | #11

    hey thx man. i had some probs too, especially because the ellipsis. ^^ Thx!

  12. Daniel
    September 22nd, 2010 at 14:10 | #12

    By the way, this conversion operates faster on large tables with a WHERE clause (PostgreSQL):

    UPDATE note
    SET note_text = REPLACE(note_text, E'\…', E'\.\.\.')
    WHERE note_text LIKE E'%\…%';

  13. Jesse
    March 27th, 2011 at 07:08 | #13

    u r a CHAMP! Thank you sir!

  14. Dan Metzner
    March 29th, 2011 at 17:45 | #14

    Thank you! I have to accommodate numerous people entering product information from various irritating sources including MS Word. This is such a simple lifesaver!

  15. Kosta Kontos
    June 3rd, 2011 at 06:56 | #15

    This is awesome! Works like a charm.
    Thanks so much 🙂
    Cheers from South Africa

  16. Eirik O.
    August 11th, 2011 at 03:31 | #16

    Thank you for this info!

    One note..
    I got an error in parsing the Ellipsis when using chr(133) in php.
    (It changed all occurrences of norwegian "Å")
    Php ord() gave 226 as the ellipsis and that worked well.

    Best Regards

  17. kbc
    October 25th, 2011 at 03:56 | #17

    Great solution, thanks for sharing

  18. gige
    November 11th, 2011 at 11:04 | #18

    Thank you mate, great smart/curly quotes, em-dashes and ellipses reference!

  19. Brandon
    December 1st, 2011 at 22:56 | #19

    Dude, you're AWESOME. This totally worked, couldn't find this info anywhere else.

  20. Mayur
    December 7th, 2011 at 23:03 | #20

    We spend about 2 hrs figuring the problem. This worked like a charm.. Very good solution

  21. December 15th, 2011 at 06:51 | #21

    Thank you! Customer has been driving me mad with this.

  22. January 29th, 2012 at 08:33 | #22

    Thank you!, quickly fixed a knotty problem.

  23. February 14th, 2012 at 17:34 | #23

    Simply awesome. Agree with many comments made. Spend the whole day.... You certainly made my day!!!! Thank you.

  24. March 25th, 2012 at 17:37 | #24

    You are seriously my hero. For my site I have different people putting descriptions in from all of these different sources and word documents. It could be a cluster eff of question-mark-filled-diamonds, but now it's filled with marvelous straight quotes and hyphens! THANK YOU. SERIOUSLY YOU HAVE SAVED ME MANY HOURS.

  25. Brinard
    April 15th, 2012 at 15:58 | #25

    Thank you!!!

  26. Heloisa
    April 23rd, 2012 at 07:57 | #26

    All my bosses were driving me crazy because of these stupid quotes, since e-mail clients do not support them and they were ruining our newsletters. Thank you so very much -)

  27. Deftbird
    September 26th, 2012 at 21:15 | #27

    Awesome, Thanks!

  28. Patrick Larson
    October 17th, 2012 at 09:16 | #28

    I really appreciate this fix. Same thing as Heloisa, newsletters built with microsoft word were not reading properly on some browsers and email clients. You saved the world a bunch of irritation.

  29. Rich
    December 4th, 2012 at 09:09 | #29

    I usually just find code examples that fix whatever my issue is and go on, but in this case I spent the better part of a day and tried 50 different things (NONE worked) until I found this....You deserve ALL the credit. Thanks you for posting.

  30. Edward
    January 23rd, 2013 at 15:41 | #30

    This has solved my issue, thank you! I tried so many alternatives and nothing else worked, then I discovered this!

  31. Vestigal
    June 5th, 2013 at 11:47 | #31

    This was very helpful; thank you!

  32. Charles
    June 20th, 2013 at 07:30 | #32

    Life Saver!

  33. Heng Chamroeun
    July 24th, 2013 at 02:30 | #33

    Great! Thank you very much!

  34. Ron
    December 12th, 2013 at 11:49 | #34

    Terrific solution, especially with curly close double quotes, as that uses a non printing ASCII 159(dec)/9D(hex) code, making it difficult to find the correct character to replace.

  35. Jan
    March 20th, 2014 at 03:14 | #35

    Thanks for your work, it saves me a lot of time.

    I'd like to note that the second part related to Windows-1252 conversion causes problem in my case. My input string is UTF8. When testing with polish 'ą' it cuts off it and all following characters?!

    Input: 'abcdąefgh'
    Output: 'abcd'

    I think that for UTF8 input it is better not to convert Windows-1252 characters. For many people it might be obvious, but I'm posting it here in case somebody was struggling with it 😉

  36. CC
    June 30th, 2014 at 04:58 | #36

    Thank you very much, you saved my life 🙂
    My PHP application saves teachers reports and then exports to a word merge and those smart quotes were crashing my program.
    Thank you again.

  37. Kenton
    August 2nd, 2014 at 20:36 | #37

    Thank you!  Your posting remains timely even to this day.  It helped me greatly to troubleshoot an ingestion issue into HBase from Ruby due to 'bad' characters.

  38. August 2nd, 2014 at 21:21 | #38

    Sweet.  🙂

  39. haluk karamete
    November 13th, 2014 at 16:56 | #39

    Another DUDE, YOU ARE AWESOME from me too!

  40. Alex
    April 19th, 2015 at 07:37 | #40

    This is 2015, and you are still god .. thanks man!

  41. Jella
    March 22nd, 2016 at 08:46 | #41

    Great mate. that really helped and saved so much time. I was getting this Right Apostrophe mark from Sql Server, i'm trying to store this in mysql database. your script solved the problem.

  42. geert
    June 13th, 2016 at 00:06 | #42


  43. Anonymous
    June 14th, 2018 at 07:00 | #43

    Thanks a million. Spent 16 hours figuring out why some of the data from my database didn't show up. Just added the PHP code an it worked. Thanks!

  44. June 14th, 2018 at 07:51 | #44

    I love you all.  🙂

If you would like a reply to your comment, you must leave your email address! We receive dozens of questions every month from people who don't leave us with any way to contact them, so we have no choice but to ignore the question. We try to reply to as many questions as we can, if we know the email address of the person who asked the question. Thanks in advance for writing in :)

Allowed HTML: <b>, <i>, <em>, <strong>. All other < and > will be replaced with &lt; and &gt;.