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.
MySQL:
# 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("'", "'", '"', '"', '-', '--', '...'), $text); // 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("'", "'", '"', '"', '-', '--', '...'), $text); |
Windows-1252 characters not present in ISO-8859-1
Further reading from Wikipedia:
UTF-8
ISO/IEC 8859-1
Windows-1252
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
I spent about 2 hours trying to figure this out. You're awesome Mango!
YOU ROCK Mango!
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
very useful - and easily extended to include \x99 and other goofiness. Many thanks.
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"
You guys be careful - you're boosting my already-inflated ego! 🙂
I am glad to hear this is helping.
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.
If Microsoft was preventing you from getting on with your life I am very glad to have been able to help with that!
Welll... I was referring to my programming life... 🙂
Thanks for this, I spent a long, long time trying to convert those stupid windows curly quotes. I really appreciate it.
hey thx man. i had some probs too, especially because the ellipsis. ^^ Thx!
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'%\…%';
u r a CHAMP! Thank you sir!
Thank you! I have to accommodate numerous people entering product information from various irritating sources including MS Word. This is such a simple lifesaver!
This is awesome! Works like a charm.
Thanks so much 🙂
Cheers from South Africa
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
Eirik
Great solution, thanks for sharing
Thank you mate, great smart/curly quotes, em-dashes and ellipses reference!
Dude, you're AWESOME. This totally worked, couldn't find this info anywhere else.
We spend about 2 hrs figuring the problem. This worked like a charm.. Very good solution
Thank you! Customer has been driving me mad with this.
Thank you!, quickly fixed a knotty problem.
Simply awesome. Agree with many comments made. Spend the whole day.... You certainly made my day!!!! Thank you.
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.
Thank you!!!
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 -)
Awesome, Thanks!
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.
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.
This has solved my issue, thank you! I tried so many alternatives and nothing else worked, then I discovered this!
This was very helpful; thank you!
Life Saver!
Great! Thank you very much!
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.
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?!
Sample:
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 😉
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.
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.
Sweet. 🙂
Another DUDE, YOU ARE AWESOME from me too!
This is 2015, and you are still god .. thanks man!
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.
Genius!
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!
I love you all. 🙂