Convert accentuated character to their ASCII equivalent in PHP
Benjamin Delespierre

Benjamin Delespierre @bdelespierre

About: I do all sorts of mischiefs with Laravel

Location:
Paris, France
Joined:
Sep 16, 2017

Convert accentuated character to their ASCII equivalent in PHP

Publish Date: Aug 6 '20
9 2

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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

Comments 2 total

  • Lars Moelleken
    Lars MoellekenAug 7, 2020
    <?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

Add comment