Limpiar cadena de caracteres


Utilizar el siguiente codigo para eliminar acentos y caracteres especiales de una string PHP

  1. /**
  2.          * @name clearString
  3.          * @description Replaces all non ANSI characters to their similar on it. If the character has no translation to ANSI code, put a underscore
  4.          * @param string $string
  5.          * @return string $string
  6.          */
  7.         private static function clear_string($string){
  8.                 $string = trim($string);
  9.                 $string = str_replace(
  10.                                 array(‘á’, ‘à’, ‘ä’, ‘â’, ‘ª’, ‘Á’, ‘À’, ‘Â’, ‘Ä’),
  11.                                 array(‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘A’, ‘A’, ‘A’, ‘A’),
  12.                                 $string );
  13.                 $string = str_replace(
  14.                                 array(‘é’, ‘è’, ‘ë’, ‘ê’, ‘É’, ‘È’, ‘Ê’, ‘Ë’),
  15.                                 array(‘e’, ‘e’, ‘e’, ‘e’, ‘E’, ‘E’, ‘E’, ‘E’),
  16.                                 $string );
  17.                 $string = str_replace(
  18.                                 array(‘í’, ‘ì’, ‘ï’, ‘î’, ‘Í’, ‘Ì’, ‘Ï’, ‘Î’),
  19.                                 array(‘i’, ‘i’, ‘i’, ‘i’, ‘I’, ‘I’, ‘I’, ‘I’),
  20.                                 $string );
  21.                 $string = str_replace(
  22.                                 array(‘ó’, ‘ò’, ‘ö’, ‘ô’, ‘Ó’, ‘Ò’, ‘Ö’, ‘Ô’),
  23.                                 array(‘o’, ‘o’, ‘o’, ‘o’, ‘O’, ‘O’, ‘O’, ‘O’),
  24.                                 $string );
  25.                 $string = str_replace(
  26.                                 array(‘ú’, ‘ù’, ‘ü’, ‘û’, ‘Ú’, ‘Ù’, ‘Û’, ‘Ü’),
  27.                                 array(‘u’, ‘u’, ‘u’, ‘u’, ‘U’, ‘U’, ‘U’, ‘U’),
  28.                                 $string );
  29.                 $string = str_replace(
  30.                                 array(‘ñ’, ‘Ñ’, ‘ç’, ‘Ç’),
  31.                                 array(‘n’, ‘N’, ‘c’, ‘C’,),
  32.                                 $string );
  33.                 //Other characters are deleted here by a ‘_’
  34.                 $string = str_replace(
  35.                                 array(\\, “¨”, “º”, “-“, “~”,
  36.                                                 “#”, “@”, “|”, “!”, \”,
  37.                                                 “·”, “$”, “%”, “&”, “/”,
  38.                                                 “(“, “)”, “?”, “‘”, “¡”,
  39.                                                 “¿”, “[“, “^”, “`”, “]”,
  40.                                                 “+”, “}”, “{“, “¨”, “´”,
  41.                                                 “>”, “< “, “;”, “,”, “:”),
  42.                                 ‘_’,
  43.                                 $string );
  44.                 return $string;
  45.         }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.