How to remove accents from a string in PHP
Removing accents (diacritics) from text comes in handy for file names or URL slugs, where you want plain letters – "e" instead of "è" or "u" instead of "ü". How to do it in PHP?
Note: Which method works depends on the encoding of the text. Websites today use UTF-8 and all examples below expect it. Old tips that pass two strings of letters to strtr() worked only in single-byte encodings such as ISO-8859-1 or Windows-1252. In UTF-8 a letter with an accent takes two or more bytes, so such code replaces single bytes and damages the text.
1. transliterator_transliterate() (the intl extension)
echo transliterator_transliterate('Any-Latin; Latin-ASCII', 'Crème brûlée, Ærøskøbing, Gdańsk, Straße'); // Creme brulee, AEroskobing, Gdansk, Strasse
echo transliterator_transliterate('Any-Latin; Latin-ASCII', 'Αθήνα, Москва'); // Athena, Moskva
The function transliterator_transliterate() converts text in any language to plain ASCII letters. It handles not only accents but also letters such as "ø" or "ß", and thanks to the Any-Latin rule even other scripts such as Greek or Cyrillic. The text must be in UTF-8 and the function needs the PHP intl extension – whether it is enabled shows extension_loaded() with the argument 'intl'.
2. Normalizer and removing combining marks
$text = Normalizer::normalize('Crème brûlée, Ærøskøbing, Gdańsk, Straße', Normalizer::FORM_D);
echo preg_replace('/\p{Mn}/u', '', $text); // Creme brulee, Ærøskøbing, Gdansk, Straße
Normalization form D (see normalizer_normalize()) splits a letter with an accent into the base letter and a separate combining mark, and preg_replace() with \p{Mn} then removes the marks. Letters that are not made of a base letter and a mark, such as "æ", "ø" or "ß", stay unchanged. This method also needs the intl extension.
3. Without intl: strtr() with an array
$map = array('à' => 'a', 'â' => 'a', 'ä' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
'î' => 'i', 'ï' => 'i', 'ñ' => 'n', 'ô' => 'o', 'ö' => 'o', 'û' => 'u', 'ü' => 'u', 'ß' => 'ss');
echo strtr('Façade, jalapeño, Müller, Straße', $map); // Facade, jalapeno, Muller, Strasse
When strtr() gets an array, it replaces whole strings, not single bytes, so it works correctly in UTF-8. It converts only the letters in the array – add the ones your texts need, including capital letters.
Why not iconv()
You will often see iconv() with the target encoding ASCII//TRANSLIT. Its result depends on the iconv library on the server and on the locale, so the same code can return different results on different servers. In PHP for Windows, for example, it returns accents as separate characters:
echo iconv('UTF-8', 'ASCII//TRANSLIT', 'Crème brûlée'); // Cr`eme br^ul'ee
If you can, use the intl extension – it gives the same result everywhere.
A URL slug
function slug($text)
{
$text = transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', $text);
$text = preg_replace('/[^a-z0-9]+/', '-', $text);
return trim($text, '-');
}
echo slug('Crème brûlée – a quick recipe!'); // creme-brulee-a-quick-recipe
The rule Lower() converts the result to lowercase, the regular expression replaces everything except letters and digits with a hyphen and trim() removes hyphens at the start and at the end.