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

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

This entry was posted in Linux How-To. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *