Remove accents from a php string

Asked

Viewed 58,531 times

11

I’m having problems with the accents in my code and I wanted to remove the accents when they did the research,

Ex: Search: Hello | Hello.

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

and brings no results, BUT when I search for Musica it returns like this:

search.php?q=Musica

with the results.

How do I get the accents out of 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>
  • 3

    See if these answers resolve your needs: http://answall.com/q/858/101 and http://answall.com/q/33029/101

  • It didn’t work or I’m not getting it fixed.

  • 1

    @feliphefelix "It didn’t work or I’m not getting it fixed" is vague and non-specific... can be more specific to know where you need help?

  • I edited the post!.

  • @flourigh rejected your issue because you tried to change the question, trying to answer it. Edits should not add anything other than the author’s intention. If you have a solution, use the answer field.

1 answer

34

$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);
  • But as I do in the code above?

  • Your code is incomplete to understand, but does so at the beginning: $_GET["q"]=tirarAcentos($_GET["q"]);

  • Apparently it does, but it’s worth noting that there are a few things missing... ý ý ç etc.

  • 4

    Simply remove accents: $file = "action-íaaa.jpg"; $file = iconv('UTF-8', 'ASCII//TRANSLIT', $file); echo "{$file} <br>"; Output: acao-iaaa.jpg Example available on ideone

Browser other questions tagged

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