Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active March 4, 2016 16:30
Show Gist options
  • Save dhaupin/9b6ea6f1e49f1d86ba46 to your computer and use it in GitHub Desktop.
Save dhaupin/9b6ea6f1e49f1d86ba46 to your computer and use it in GitHub Desktop.
Function - Filter PDF/print rogue special characters from WYSIWYG "full-paste" or rando DB source
<?php
// @last https://3v4l.org/0LHkO
// Start sample
$str = '® ‡ ¼';
echo clean($str, false, false);
// End sample
function clean($str, $strict = true, $punct = true) {
$base_pool = array(
'‘' => '&lsquo;',
'’' => '&rsquo;',
'‚' => '&sbquo;',
'“' => '&ldquo;',
'”' => '&rdquo;',
'„' => '&bdquo;',
'©' => '&copy;',
'®' => '&reg;',
'™' => '&trade;',
'°' => '&deg;',
'•' => '&bull;',
'–' => '&ndash;',
'—' => '&mdash;',
'…' => '&#8230;',
'¼' => '&#188;',
'½' => '&#189;',
'¾' => '&#190;',
'‰' => '&#8240;',
'±' => '&#177;',
'†' => '&#8224;',
'‡' => '&#8225;',
'&nbsp;' => ' ',
'&amp;amp;' => '&amp;'
);
$strict_pool = array(
'\'' => '&#39;',
'"' => '&quot;',
"\r" => ' ',
"\n" => ' ',
"\t" => ' '
);
// if strict param is true we will clean harder (default true)
if ($strict) {
$base_pool = array_merge($strict_pool, $base_pool);
}
foreach ($base_pool as $key => $value) {
$str = str_replace($key, $value, $str); // iterate through pool key => values and replace in $str
}
// if punct param is true we will add spaces after punctuation (default true)
if ($punct) {
$str = preg_replace('/([!?,.])(\S)/', '$1 $2', $str); // parse punctuation and add trail space
}
$str = preg_replace("/\s+/", ' ', $str); // filter multiple spaces into single
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment