Objective

How to remove all accents of a PHP string.

Code

/**
 * Remove accents of a string
 */
function removeAccents($string, $charset="utf-8")
{
    $string = htmlentities($string, ENT_NOQUOTES, $charset);
    $string = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $string);
    $string = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $string); // ligatures e.g. 'œ'
    $string = preg_replace('#&[^;]+;#', '', $string); // remove escaped characters that are left
   
    return $string;
}

Comments

  • The idea is to escape all special characters first, then to replace them with a Regex
  • Better solution than the non-consistent iconv() PHP function

Source

 

Keep Me Informed

Keep me informed of the latest extensions releases and offers. One email maximum per month.

Offer Me A Coffee!

If you like my work, offer me a coffee! Nick.