How to remove accents?

Asked

Viewed 964 times

1

<?php
$string = $_POST["nome1"];
function removeAcentos($string, $slug = false){
    $string = strtolower($string);
    $ascii['a'] = range(224, 230);
    $ascii['e'] = range(232, 235);
    $ascii['i'] = range(236, 239);
    $ascii['o'] = array_merge(range(242, 246), array (240, 248));
    $ascii['u'] = range(249, 252);
    $ascii['b'] = array(223);
    $ascii['c'] = array(231);
    $ascii['d'] = array(208);
    $ascii['n'] = array(241);
    $ascii['y'] = array(253, 255);
    foreach ($ascii as $key=>$item) {
        $acentos = '';
        foreach ($item as $codigo) $acentos .= chr($codigo);
        $troca[$key] = '/['.$acentos.']/i';
    }
    $string = preg_replace(array_values($troca), array_keys($troca), $string);
    return $string;
}
?>
<html>
    <body>
        <form action="" method="post">
            Nome: 
            <input type="text" name="nome1">
            <br>
            <input type="hidden" name="nome2" value='$string'>
            <br>
            <input type="submit" value="OK">
        </form>
        Nome: 
        <?php echo $_POST["nome1"]; ?><br>
        Nome trocado: 
        <?php echo removeAcentos($_POST['nome1']; ?>
    </body>
</html>
  • try this way https://answall.com/a/33035/8939 with this feature, you may remove accent at all

1 answer

2

If you want to replace the special characters with their normal correspondents, I use this function:

function limpaString($str) { 
    $str = preg_replace('/[áàãâä]/ui', 'a', $str);
    $str = preg_replace('/[éèêë]/ui', 'e', $str);
    $str = preg_replace('/[íìîï]/ui', 'i', $str);
    $str = preg_replace('/[óòõôö]/ui', 'o', $str);
    $str = preg_replace('/[úùûü]/ui', 'u', $str);
    $str = preg_replace('/[ç]/ui', 'c', $str);
    return $str;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.