[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2499
  • Last Modified:

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
0
BenMorel
Asked:
BenMorel
1 Solution
 
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now