If outputting UTF-8 content, this seems to work well:
<?php
function filterText($text)
{
$search = array (
'&',
'<',
'>',
'"',
chr(212),
chr(213),
chr(210),
chr(211),
chr(209),
chr(208),
chr(201),
chr(145),
chr(146),
chr(147),
chr(148),
chr(151),
chr(150),
chr(133)
);
$replace = array (
'&',
'<',
'>',
'"',
'&quot;',
'&#8216;',
'&#8217;',
'&#8220;',
'&#8221;',
'&#8211;',
'&#8212;',
'&#8230;',
'&#8216;',
'&#8217;',
'&#8220;',
'&#8221;',
'&#8211;',
'&#8212;',
'&#8230;'
);
return str_replace($search, $replace, $text);
}
$test = <<<END
This here’s a test. “It is only a test.” ‘The end.’
END;
header('Content-Type: text/html; charset="UTF-8"');
echo filterText($test);