I receive from my MySQL database a multidimensional array
Array
(
[0] => Array
(
[page] => categorypropose
[value] => baby-sitters
[id] => 357960
)
[1] => Array
(
[page] => categorysearch
[value] => adéquate pour garder
[id] => 357961
)
...
)
In this array, I have some ISO-8859-1 to UTF8 conversion to do via a ‘homemade’ function “loadtext”.
But when I do this :
$array = $query->result_array();
foreach($array as &$k)
{
foreach ($k as &$value)
{
//Works
$value = $this->loadtext($value, 'ISO-8859-1');
}
}
//Back to normal as $this->loadtext never existed
print_r($array);
It doesn’t conserve the changes (When I echo $value, it works, but the modification is not kept at the end …)
EDIT : This is the function loadtext that I am oblige to use (actually, I didn’t make it but I have to use it …)
function loadtext($text,$charset){
$text = stripslashes($text);
if($charset!="UTF-8")
$text = iconv("UTF-8",$charset,$text);
$text = str_replace(" :"," :",$text);
$text = str_replace(" ;"," ;",$text);
$text = str_replace(" !"," !",$text);
$text = str_replace(" ?"," ?",$text);
$text = str_replace(" ."," .",$text);
$text = str_replace(" …"," …",$text);
return $text;
}
You could try it like this:
But better yet, you could try using the MySQL function
CONVERT()in your query so that the strings you get back are already in UTF8 format.http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html
At the very least, use PHP’s
mb_convert_encoding()instead of your homemade function. There’s no reason to reinvent the wheel.https://www.php.net/manual/en/function.mb-convert-encoding.php