Convert accented characters to their plain ascii equivalents
Solution 1
The PHP Manual iconv Intro has a warning:
Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the GNU libiconv library. It will most likely end up with more consistent results.
But if accented characters are the only issue then you could use a dirty strtr (partially from strtr comments):
$string = 'Ë À Ì Â Í Ã Î Ä Ï Ç Ò È Ó É Ô Ê Õ Ö ê Ù ë Ú î Û ï Ü ô Ý õ â û ã ÿ ç';
$normalizeChars = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'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', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
);
//Output: E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a u a y c
echo strtr($string, $normalizeChars);
Solution 2
This worked for me for French characters.
$str = utf8_encode($str);
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
Solution 3
An alternative:
function replaceAccents($str) {
$search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,Ø,Å,Á,À,Â,Ä,È,É,Ê,Ë,Í,Î,Ï,Ì,Ò,Ó,Ô,Ö,Ú,Ù,Û,Ü,Ÿ,Ç,Æ,Œ");
$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,o,O,A,A,A,A,A,E,E,E,E,I,I,I,I,O,O,O,O,U,U,U,U,Y,C,AE,OE");
return str_replace($search, $replace, $str);
}
$str = "À é ü ä ç";
$str = replaceAccents($str);
echo "$str \n";
//output "A e u a c"
Solution 4
Here is the wordpress way:
http://codex.wordpress.org/Function_Reference/remove_accents
You can copy the remove_accents() function and implement to your system.
https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/formatting.php#L682
Solution 5
Here's what helped me. It's important to set locale to en_US and not your real locale.
$curLocale = setlocale(LC_ALL, 0); //gets current locale
setlocale(LC_ALL, "en_US.utf8"); //without this iconv removes accented letters. If you use another locale it will also fail
$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
setlocale(LC_ALL, $curLocale); //set locale to what it was before
ram
Updated on May 12, 2020Comments
-
ram almost 4 years
I have to convert french characters into english on my php. I've used the following code:
iconv("utf-8", "ascii//TRANSLIT", $string);
But the result for
ËËË
was"E"E"E
.I don't need that double quote and other extra characters - I want to show an output like
EEE
. Is there any other method to convert french to english? Can you help me to do this? -
ram about 12 yearsIf i got my string from html form(ex:ÉÉÉabcd) and used, $this->_data = JRequest::get('post'); $string=$this->_data['quiz_optionA']; Then used that convertion method the result was A�A�A�abcd..The french characters didnt coverted as a string.Output should be EEEabcd.U have any idea to do this ?
-
ram about 12 yearsÉÉÉabcd.This is my string,which comes from form, using post method.
-
Ingmar Boddington about 12 yearsAfter this line?
$string=$this->_data['quiz_optionA'];
If you just try and output the string as is, does it break the accented characters? -
ZeWaren almost 11 yearsActually the result is not the same on Windows and on Linux.
-
Matt Fletcher over 10 yearsReplaces Irish characters, such as
Í
with?
when I tried it... is there a difference between those and French chars? -
Matt Fletcher over 10 yearsActually, the first line converted the character to
Ã
and then the second line converted theÃ
to??
:/ -
Matt Fletcher over 10 yearsIt's dirty, but it works... at least for me when converting Irish names!
-
joshweir about 8 yearsmissing 'ń' => 'n'
-
Josh Bernfeld almost 8 yearsThis fixed the question marks and quotes for me
setlocale(LC_ALL, "en_US.utf8"); $string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
-
rap-2-h over 6 years"You can copy the remove_accents() function ..." I created a package by roughly copy/paste the wordpress function: github.com/rap2hpoutre/convert-accent-characters Hope it could help!
-
ecabuk over 6 years@rap-2-h Wordpress is licensed with GPL, correct me if I am wrong but your package should also have same license.
-
Matt Harley over 5 years
str_slug
also supports a$separator
parameter so you can dostr_slug($accentedPhrase, ' ')
as a shortcut -
Round-Sliced almost 5 yearsyou can just open the file from link and rip out their character map when you scroll enough....
-
Maciej Krawczyk about 2 yearsPolish special chars are missing.
-
Pryftan about 2 yearsNote: In German if you don't have access to the umlaut you can add an E: for example Ü becomes UE; ö becomes oe; etc. So the trick is deciding whether or not you can always do that. I don't know if this rule applies to languages other than German though and that's the problem in the first place: not everything translates okay.