Code Snippets jQuery

Replace Smart Quotes With Straight Quotes

I created a web app that uses ckEditor as the rich text editor. It works perfectly except when you copy and paste text that includes smart quotes and other special symbols. CkEditor converts the smart quotes to HTML entities, which would be fine in a normal situation. The Html entities caused a problem when it was time to write the iTunes XML feed. So what I ended up doing is replacing all the html entities with the correct character as soon as I got the content from ckEditor. The code I used to replace smart quotes with straight quotes is below.

function cleanstring(dirty){
	var smartchr = [ "’","‘","“","”","–","—","…", " ", '„', '‚' , '«','»', '‹', '›'];
	var correctchr = ["'", "'", '"', '"', '-', '-', '...', '', '"', "'", '"', '"', "'", "'"];

	var thestring = dirty;
	var regex; 
  	for (var i = 0; i < smartchr.length; i++) {
    	regex = new RegExp(smartchr[i], "g");
    	thestring = thestring.replace(regex, correctchr[i]);

	return thestring;

Subscribe To Updates and Tips Via Email

Get tips on web development, WordPress, web design & more