Using the functions strtr() or str_ireplace() or preg_replace() with array_walk_recursive(), I try to delete the bad character encoding in a multidimensional array, the data are encoding in UTF-8 and comes from a Curl query.
I want to remove the double encoding by keeping only the correctly encoded accented character:
ã©école => école
Array
(
[0] => Array
(
[0] => ã©cole
[1] => Array
(
[0] => ã©ecole al inara avenue 2 mars casablanca
[1] => ã©ecole 42
[2] => grande ã©école
)
)
)
With PHP 7.2.6 i get an error with my code when I do this, is it a bad way to proceed?
Fatal error: Uncaught ArgumentCountError: Too few arguments to function
function fix_utf8(&$value, $key)
{
$char = array('é','É','è','ê','ë','Ã','à¢','ù','î','ô','ë','ö','ü','à»','ç','à§','Å“','’','…','Å“','–','«','»','‚');
$value = str_ireplace($char, '', $value);
}
$result = array_walk_recursive($result, 'fix_utf8');
print_r($result);
OR
Fatal error: Uncaught ArgumentCountError: Too few arguments to function fix_utf8(), 1 passed in
function fix_utf8(&$value, $key)
{
$char = array('é'=>'','É'=>'','è'=>'','ê'=>'','ë'=>'','Ã'=>'','à¢'=>'','ù'=>'','î'=>'','ô'=>'','ë'=>'','ö'=>'','ü'=>'','à»'=>'','ç'=>'','à§'=>'','Å“'=>'','’'=>'','…'=>'','Å“'=>'','–'=>'','«'=>'','»'=>'','‚'=>'');
$value = strtr(strtoupper($value), $char);
}
$result = array_walk_recursive($result, 'fix_utf8');
print_r($result);
OR
function fix_utf8(&$value, $key)
{
$char = array('/é/','/É/','/è/','/ê/','/ë/','/Ã/','/à¢/','/ù/','/î/','/ô/','/ë/','/ö/','/ü/','/à»//','//ç/','/à§/','/Å“/','/’/','/…/','/Å“/','/–/','/«/','/»/','/‚/');
$value = preg_replace($char, '', $value);
}
$result = array_walk_recursive($result, 'fix_utf8');
print_r($result);
Update:
Precision: the CuRL request retrieves content formatted in JSON and containing Unicode characters
["école",["école d\u0027ingénieur"]]