Sacar acentos y caracteres raros con PHP

// July 16th, 2009 // Programacion

Esta función sirve para limpiar un string de caracteres raros o acentos

function reemplazar($string)
{
$string = ereg_replace("[áàâãª]","a",$string);
$string = ereg_replace("[ÁÀÂÃ]","A",$string);
$string = ereg_replace("[ÍÌÎ]","I",$string);
$string = ereg_replace("[íìî]","i",$string);
$string = ereg_replace("[éèê]","e",$string);
$string = ereg_replace("[ÉÈÊ]","E",$string);
$string = ereg_replace("[óòôõº]","o",$string);
$string = ereg_replace("[ÓÒÔÕ]","O",$string);
$string = ereg_replace("[úùû]","u",$string);
$string = ereg_replace("[ÚÙÛ]","U",$string);
$string = str_replace("ç","c",$string);
$string = str_replace("Ç","C",$string);
$string = str_replace("ñ","n",$string);
$string = str_replace("Ñ","N",$string);
return $string;
}

3 Responses to “Sacar acentos y caracteres raros con PHP”

  1. Miguel says:

    Buena aportación aunque le faltaria unas lineas mas con caracteres como interrogacion, comillas, menor que etc etc

    saludos

  2. Esta función completaría los caracteres especiales.
    Espero les sirva . Gracias

    function _only_chars($text){
    $text = ereg_replace(”[áàâãª]“,”a”,$text);
    $text = ereg_replace(”[ÁÀÂÃ]“,”A”,$text);
    $text = ereg_replace(”[ÍÌÎ]“,”I”,$text);
    $text = ereg_replace(”[íìî]“,”i”,$text);
    $text = ereg_replace(”[éèê]“,”e”,$text);
    $text = ereg_replace(”[ÉÈÊ]“,”E”,$text);
    $text = ereg_replace(”[óòôõº]“,”o”,$text);
    $text = ereg_replace(”[ÓÒÔÕ]“,”O”,$text);
    $text = ereg_replace(”[úùû]“,”u”,$text);
    $text = ereg_replace(”[ÚÙÛ]“,”U”,$text);
    $text = str_replace(”ç”,”c”,$text);
    $text = str_replace(”Ç”,”C”,$text);
    $text = str_replace(”ñ”,”n”,$text);
    $text = str_replace(”Ñ”,”N”,$text);

    $text = preg_replace(”/[^A-Za-z0-9 _\.]/”,”",$text);
    return $text;
    }

  3. rodrigo says:

    Una forma mas abreviada seria:
    function elimina_acentos($text){
    $text=utf8_decode($text);
    $tofind = utf8_decode(’ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ’);
    $replac = ‘AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn’;
    $text= strtr($text, $tofind, $replac);
    $text = preg_replace(”/[^A-Za-z0-9 _]/”,”",$text);
    return $text;
    }

Leave a Reply