Remove accents in a UTF-8 string (PHP)

Hello,
I'm looking for a way to remove accents from a UTF-8 string.
Solutions involving transformation to Latin-1 then back to UTF-8 are not acceptable.
Solutions involving hardcoding character tables are not acceptable.
Basically I'm looking for a clean way to do it, with well-known PHP libraries if possible.

Thanks !
Ben
LVL 7
BenMorelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NeoAshuraCommented:
hello hopw this is what your looking for

$title = "Trípode G5";
$search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
$replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
$urlTitle = str_replace($search, $replace, $title);


http://www.randomsequence.com/articles/removing-accented-utf-8-characters-with-php/
0
NeoAshuraCommented:
alternatively you could try this function..
<?php

function normalize ($string) {
    $table = array(
        'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'd'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'R'=>'R', 'r'=>'r',
    );
   
    return strtr($string, $table);
}

?>

Open in new window

0
BenMorelAuthor Commented:
Please read the question : Solutions involving hardcoding character tables are not acceptable.
Thanks.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

dactechsCommented:
Good luck with that. No solutions involving the solution are acceptable. Without character tables, I do not think there is a solution. Accents are not like removable attributes to be applied or removed from characters. Accented characters are simply specific characters within a character table.

0
NeoAshuraCommented:
i wasnt going to say that as i thought there may possibly be a way but i thought it was very unlikely.
0
BenMorelAuthor Commented:
No offense here. I just don't want a basic character table, which will inevitably be incomplete (your tables above are very incomplete). What I'm looking for is either using a PHP extension such as intl, or well-known (pure) PHP libraries, that are known to be rather complete, and, what's most important, that will be maintained in the future. Not just a basic table that we don't have any idea whether it is complete or not !
0
BenMorelAuthor Commented:
I just found the solution below, however because it is designed to transliterate the string, it prepends chars with modifiers, such as `, " or '
Without this behavior, it would fit my needs.
Any other idea ?
iconv("utf-8", "ascii//TRANSLIT", $string); 

Open in new window

0
Rok-KraljCommented:
Just strip these characters out of the string... Use str_replace()... If you are not ok with that, I'll write you a better function.
0
BenMorelAuthor Commented:
Of course, but this will also remove legitimate apostrophes, etc.
This is unfortunately not acceptable either !
0
BenMorelAuthor Commented:
Another approach... Still not perfect because it removes some legitimate utf8 chars which cannot be translated to non-accented chars !
$string = Normalizer::normalize($string, Normalizer::FORM_D);
$string = preg_replace('/[\\x80-\\xFF]+/', '', $string);

Open in new window

0
ee_autoCommented:
Question PAQ'd, 500 points refunded, and stored in the solution database.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.