Alex Poole

Pragmatic PHP, MySQL, Marketing & Technology

Alex Poole header image 2

Remove “Smart” quotes, bullets, dashes and other junky characters from a string with PHP

March 31st, 2010 by alex

AAAAArgghh!
Why do some Word Processors spew out such a pile of bloated incompatible rubbish?!
If you’ve got any type of CMS and your web pages are showing “junk character” symbols (usually an incongruent question mark of some type,) likelihood is the string you are echoing contains some type of “smart” characters.
There’s a misnomer if ever there was one. Regardless. Onwards.
Try this function:

//replace smart quotes
function convert_smart_quotes($string)
{
$search = array(chr(145),
chr(146),
chr(147),
chr(148),
chr(151),
chr(150),
chr (133),
chr(149)

);

$replace = array("'",
"'",
'"',
'"',
'--',
'-',
'...',
"•"

);
return str_replace($search, $replace, $string);
}

I pinched bits of it from these pages:

http://www.keepandshare.com/doc2/3693/turning-off-microsoft-smart-quotes-or-stipping-programmatically

http://www.liamdelahunty.com/tips/remove_special_characters.php

Cheers.

Tags: 3 Comments

Leave a Comment

3 responses so far ↓