Modify search to ignore accents

I am working on a weather website and right now my search works likes this:
I write the city name (“Lima”) in the searchbox and it takes you to URL (www.peruclima.com/ciudad/lima)
It works even with cities that have more than one word (“La Libertad”) and replaces the space between words with a hyphen (www.peruclima.com/ciudad/la-libertad)
What I want know is for it to ignore accents. F.e. If I write “Ancón” or “Ancon” in the searchbox, it should take me to the same place (www.peruclima.com/ciudad/ancon)
This is what the code looks like right now (including replacing space with a hyphen)… so how could I make it ignore accents? Thanks!

[code]$bus=$_GET[city];
$bus=strtolower($bus);

$search = array(" “);
$replace = array(”-");
$result = str_replace($search, $replace,$bus);
[/code]

Hi, I know it’s a very old post, but if anyone’s searching for an answer for something similar, I believe you can solve it in two ways:

  1. Create a map with accented characters and replace them with the ones you have caught:

$translationArray = ['ă'=>'a', 'Ă'=>'A'] // etc

And then use strtr:

strtr('My accented string', $translationArray);

The second way is to use iconv:
iconv('UTF-8','ASCII//TRANSLIT', 'My accented string');

Here’s the documentation reference:
https://www.php.net/manual/en/function.iconv.php
https://www.php.net/manual/en/function.strtr

Sponsor our Newsletter | Privacy Policy | Terms of Service