Remove accents from a string in php [duplicate]

8

To have problems with the accents in my code and wanted to remove the accents when doing the search,

Ex: Search: Hello | Hello.

When doing a Music search on the site it returns the url like this: search.php? q = Music

And it does not bring any results, BUT when I search for Music it returns like this:

search.php?q=Musica

with the results.

How do I get the accents in the code below:

<div class="search_result">
<div class="image-container">
  <a href="' . $song[0] . '/'.str_replace(' ','-',strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $song[1]), '-'))).'.html"><img src="'. $song[2] . '"></a>
</div>
<div class="search-container">
  <div class="lead"><a href="' . $song[0] . '/'.str_replace(' ','-',strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $song[1]), '-'))).'.html">' . $song[1] . '</a></div>
  <div class="search-duration">'.$lang['search_description'].' '.$description.'</div>
  <div class="search-duration">'.$lang['search_duration'].' '. $song[4] .'</div>
</div>
</div>
    
asked by anonymous 05.02.2015 / 23:51

1 answer

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