r/PHPhelp • u/[deleted] • Jan 30 '20
Encoding issue, trying to replace strings but getting a way different result
<?php
function Utf8_ansi($valor) {
$utf8_ansi2 = array(
"\u00c0" =>"À",
"\u00c1" =>"Á",
"\u00c2" =>"Â",
"\u00c3" =>"Ã",
"\u00c4" =>"Ä",
"\u00c5" =>"Å",
"\u00c6" =>"Æ",
"\u00c7" =>"Ç",
"\u00c8" =>"È",
"\u00c9" =>"É",
return strtr($valor, $utf8_ansi2);
}
$test='\u00e9", ';
echo Utf8_ansi($test);
?>My output is és",
Any ideas?
7
Upvotes
1
1
u/lchoate Jan 30 '20
function Utf8_ansi($valor) {
$utf8_ansi2 = [
"\u00c0" =>'À',
"\u00c1" =>"Á",
"\u00c2" =>"Â",
"\u00c3" =>"Ã",
"\u00c4" =>"Ä",
"\u00c5" =>"Å",
"\u00c6" =>"Æ",
"\u00c7" =>"Ç",
"\u00c8" =>"È",
'\u00c9' =>'É',
];
return strtr($valor, $utf8_ansi2);
}
$test='\u00c9';
echo Utf8_ansi($test); 1
1
u/jedifans Jan 30 '20
https://www.php.net/manual/en/function.mb-convert-encoding.php can handle this conversion a lot more safely.
ANSI is Windows-1252 in https://www.php.net/manual/en/mbstring.supported-encodings.php