Advertisement
adfaklsdjf

fix characters simpler

Aug 11th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. /**
  2.  * An array of characters that we want to convert
  3.  */
  4. function get_bad_characters() {
  5.  
  6.   static $find;
  7.  
  8.   if ( ! is_array($find) ) {
  9.     $find = array();
  10.     $find[] = '“';      // left side double smart quote
  11.     $find[] = '”';       // right side double smart quote
  12.     $find[] = '‘';      // left side single smart quote
  13.     $find[] = '’';      // right side single smart quote
  14.     $find[] = '…';      // elipsis
  15.     $find[] = '—';      // em dash
  16.     $find[] = '–';      // en dash
  17.     $find[] = '’'; // quote mark
  18.     $find[] = 'â€"';   // ndash I think
  19.     $find[] = 'â€Â';   // close right double quote
  20.     $find[] = '·';     // small filled dot or small black bullet
  21.     $find[] = '"¢';       // regular bullet
  22.     $find[] = '�';   // apostrophe
  23.   }
  24.  
  25.   return $find;
  26. }
  27.  
  28. /**
  29.  * An array of replacement characters that map in order to the ones from bad_characters()
  30.  */
  31. function get_replacements() {
  32.   static $replacements;
  33.  
  34.   if ( ! is_array($replacements) ) {
  35.     $replacements = array();
  36.     $replacements[] = '"';
  37.     $replacements[] = '"';
  38.     $replacements[] = "'";
  39.     $replacements[] = "'";
  40.     $replacements[] = "...";
  41.     $replacements[] = "-";
  42.     $replacements[] = "-";
  43.     $replacements[] = "'";
  44.     $replacements[] = "–";
  45.     $replacements[] = '"';
  46.     $replacements[] = "·";
  47.     $replacements[] = "•";
  48.     $replacements[] = "'";
  49.   }
  50.  
  51.   return $replacements;
  52. }
  53.  
  54. function fix_characters($string) {
  55.   $string = str_replace(get_bad_characters(), get_replacements(), $string);
  56.   return $string;
  57. }
  58.  
  59. function fix_characters_simpler($string) {
  60.   $map = array(
  61.     '“'       => '"',
  62.     '”'        => '"',
  63.     '‘'       => "'",
  64.     '’'       => "'",
  65.     '…'       => "...",
  66.     '—'       => "-",
  67.     '–'       => "-",
  68.     '’'  => "'",
  69.     'â€"'    => "–",
  70.     'â€Â'    => '"',
  71.     '·'      => "·",
  72.     '"¢'        => "•",
  73.     '�'    => "'",
  74.   );
  75.   return str_replace(array_keys($map), array_values($map), $string);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement