Microsoft Smart Quotes & PHP
It seems like I run into this issue for over half of the websites that I work on. The user wants to copy-and-paste their article, document, or whatever from MS Word, into a textarea and save it. The problem is word uses funky quotes, dashes, and other characters. Once it's submitted, PHP gets it and the characters are encoded differently and they display weird.
We've tried several different methods to try to eliminate the problem, but every time I googled for a fix, I never really found anything that worked well.
My boss mentioned that maybe we could fix it on the client-level, so I dove in and found what seems to be a promising fix. It's clean and simple. This function currently only replaces the single and double smart quotes, and then the strange dash character that MS word uses. Feel free to submit for character conversion codes and I'll add it to the function, I'll also add to this once I come across more problems.
Javascript function to replace Microsoft Smart Quotes with regular quotes.
function removeMSWordChars(str) { var myReplacements = new Array(); var myCode, intReplacement; myReplacements[8216] = 39; myReplacements[8217] = 39; myReplacements[8220] = 34; myReplacements[8221] = 34; myReplacements[8212] = 45; for(c=0; c<str.length; c++) { var myCode = str.charCodeAt(c); if(myReplacements[myCode] != undefined) { intReplacement = myReplacements[myCode]; str = str.substr(0,c) + String.fromCharCode(intReplacement) + str.substr(c+1); } } return str; }
This is the jQuery that will run the filter on all textareas on your page when you tab away from the textarea (Assumes you have jQuery installed and running on the page.)
$(function(){ $("textarea").blur(function(){ $(this).val(removeMSWordChars(this.value)); }); });
Removing smart quotes javascript example
Or if you don't use jQuery and you're a little green to javascript you can do this:
<textarea onBlur="this.value=removeMSWordChars(this.value);" name="a" rows=5 cols=10></textarea>

6 comments to "Replace Microsoft Word smart quotes and other characters with Javascript"
jQuery syntax is a bit different if you're not used to using it, but it's very powerful. Hope that answers your question.
Leave a Comment