loading...

Convert accentuated character to their ASCII equivalent in PHP

bdelespierre profile image Benjamin Delespierre ・1 min read

A a french developer, I often come across non-ASCII characters in user-input data. In order to generate clean, search friendly equivalents, I created the following function that removes the accents while preserving the string integrity.

Example

$str = "À l'île, en été, quelle félicité !";

echo accent2ascii($str); // A l'ile, en ete, quelle felicite

The function

/**
 * Converts accentuated characters (àéïöû etc.) 
 * to their ASCII equivalent (aeiou etc.)
 *
 * @param  string $str
 * @param  string $charset
 * @return string
 */
function accent2ascii(string $str, string $charset = 'utf-8'): string
{
    $str = htmlentities($str, ENT_NOQUOTES, $charset);

    $str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
    $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. 'œ'
    $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères

    return $str;
}

Don't forget to leave a like to encourage me to post more useful PHP snippets.

Posted on by:

bdelespierre profile

Benjamin Delespierre

@bdelespierre

CTO at AddWorking, I write Laravel snippets all the time

Discussion

markdown guide
 
<?php

$str = "À l'île, en été, quelle félicité !";

var_dump(transliterator_transliterate('NFKC; [:Nonspacing Mark:] Remove; NFKC; Any-Latin; Latin-ASCII', $str));

(3v4l.org/H3DAb)

If the "intl" php extension is not installed or you need something language specific you can also use this package: github.com/voku/portable-ascii