Advertisement
jacknpoe

Conversões de caracteres, letras, números, etc

Jun 25th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. //  A TABELA DE CONVERSÃO E OS MÉTODOS DEVERÃO ESTAR EM UMA CLASSE (ORIENTADOS A OBJETO)
  2. //  PARA USAR FORA DE CLASSE, RETIRAR OS QUALIDICADORES (private) E O PONTEIRO ESPECIAL ($this->)
  3.  
  4. //  CONVERSÃO DE LETRAS (CUIDADO, NÃO EXISTE "DEFAULT" PARA LETRAS NÃO-ASCII, VER ABAIXO)
  5.  
  6. private $_tabela_conversao = array(
  7.     'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  8.     'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
  9.     'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
  10.     'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
  11.     'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
  12.     'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y',
  13.     'þ'=>'b', 'ÿ'=>'y', '°'=>'o', 'º'=>'o', 'ª'=>'a'
  14.         );
  15.         // retirado (e adaptado) do manual do PHP (allixsenos at gmail dot com)
  16.         // ADICIONAR QUALQUER OUTRA CONVERSÃO AQUI
  17.         // exemplos de alguns símbolos que podem exigir conversão/tratamento:
  18.         // !@"#$%-=*&,.;:/?´^~_§'=<>()|\
  19.  
  20. private function converterAcentosEEspeciais( $texto)
  21. {
  22.     return strtr( $texto, $this->_tabela_conversao);  // VER TABELA (array) NO TOPO
  23. }
  24.     // retirado (e adaptado) do manual do PHP (allixsenos at gmail dot com)
  25.  
  26.  
  27. //  CONVERTE AS LETRAS SEGUNDO A TABELA NO TOPO (OPCIONAL) E ACEITA APENAS LETRAS E NÚMEROS
  28.  
  29. private function apenasLetrasENumeros( $texto, $converter)
  30. {
  31.     if( $converter)
  32.     {
  33.         $texto = $this->converterAcentosEEspeciais( $texto);
  34.     }
  35.  
  36.     $texto_temp = '';
  37.  
  38.     for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  39.     {
  40.         $ascii = ord( $texto[$contador]);
  41.  
  42.         //  OUTROS MÉTODOS PODEM SER CRIADOS PARA ACEITAR OUTROS CARACTERES
  43.         //  ACRESCENTE LINHAS "OR" COMO ABAIXO COM AS FAIXAS CORRESPONDENTES
  44.  
  45.         //  EXERCÍCIO: CRIAR MÉTODOS COM OPÇÕES DE FAIXAS COMO PARÂMETRO
  46.  
  47.         if (    ( $ascii >= 97 and $ascii <= 122 )
  48.             or ( $ascii >= 65 and $ascii <= 90 )
  49.             or ( $ascii >= 48 and $ascii <= 57 ) )
  50.         {
  51.             $texto_temp .= $texto[$contador];
  52.         }
  53.     }
  54.  
  55.     return $texto_temp;
  56. }
  57.  
  58.  
  59. //  CONVERTE AS LETRAS SEGUNDO A TABELA NO TOPO (OPCIONAL) E ACEITA APENAS LETRAS
  60.  
  61. private function apenasLetras( $texto, $converter)
  62. {
  63.     if( $converter)
  64.     {
  65.         $texto = $this->converterAcentosEEspeciais( $texto);
  66.     }
  67.  
  68.     $texto_temp = '';
  69.  
  70.     for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  71.     {
  72.         $ascii = ord( $texto[$contador]);
  73.  
  74.         if (    ( $ascii >= 97 and $ascii <= 122 )
  75.             or ( $ascii >= 65 and $ascii <= 90 ) )
  76.         {
  77.             $texto_temp .= $texto[$contador];
  78.         }
  79.     }
  80.  
  81.     return $texto_temp;
  82. }
  83.  
  84.  
  85. //  CONVERTE AS LETRAS SEGUNDO A TABELA NO TOPO (OPCIONAL) E ACEITA APENAS NÚMEROS
  86.  
  87. private function apenasNumeros( $texto, $converter)
  88. {
  89.     if( $converter)
  90.     {
  91.         $texto = $this->converterAcentosEEspeciais( $texto);
  92.     }
  93.  
  94.     $texto_temp = '';
  95.  
  96.     for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  97.     {
  98.         $ascii = ord( $texto[$contador]);
  99.  
  100.         if ( $ascii >= 48 and $ascii <= 57 )
  101.         {
  102.             $texto_temp .= $texto[$contador];
  103.         }
  104.     }
  105.  
  106.     return $texto_temp;
  107. }
  108.  
  109.  
  110. //  CONVERTE AS LETRAS SEGUNDO A TABELA NO TOPO (OPCIONAL) E ACEITA APENAS ASCII (ENTRE 32 e 126)
  111.  
  112. private function apenasASCII( $texto, $converter)
  113. {
  114.     if( $converter)
  115.     {
  116.         $texto = $this->converterAcentosEEspeciais( $texto);
  117.     }
  118.  
  119.     $texto_temp = '';
  120.  
  121.     for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  122.     {
  123.         $ascii = ord( $texto[$contador]);
  124.  
  125.         // SERÃO ACEITOS CARACTERES ASCII ENTRE 32 E 126
  126.  
  127.         if ( $ascii >= 32 and $ascii <= 126 )
  128.         {
  129.             $texto_temp .= $texto[$contador];
  130.         }
  131.  
  132.         // OUTROS CARACTERES SERÃO ALTERADOS PARA ESPAÇO
  133.         // EXEMPLO PARA ALTERAR PARA UNDERLINE:  $texto_temp .= '_';
  134.         // PARA IGNORAR OS CARACTERES, COMENTAR A CLÁUSULA ELSE ABAIXO
  135.  
  136.         else
  137.         {
  138.             $texto_temp .= ' ';
  139.         }
  140.     }
  141.  
  142.     return $texto_temp;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement