Removing spaces and accents with PHP

0

I need to remove the spaces and accents of a word with the function below, but only the accents are removed. How do I remove the spaces too?

$string="João é de Maranhão";
function tirarAcentos($string){
    return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"),$string);
}
echo tirarAcentos($string); // retorno Joao e de Maranhao
    
asked by anonymous 17.01.2018 / 12:13

1 answer

2

Use the str_replace function to take the space before taking the accents:

$string="João é de Maranhão";
function tirarAcentos($string){
    return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"), str_replace(" ", "", $string));
}
echo tirarAcentos($string); // retorno Joao e de Maranhao
    
17.01.2018 / 12:21