'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel json json

'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel


I wrote this method to handle UTF8 arrays and JSON problems. It works fine with array (simple and multidimensional).

/** * Encode array from latin1 to utf8 recursively * @param $dat * @return array|string */   public static function convert_from_latin1_to_utf8_recursively($dat)   {      if (is_string($dat)) {         return utf8_encode($dat);      } elseif (is_array($dat)) {         $ret = [];         foreach ($dat as $i => $d) $ret[ $i ] = self::convert_from_latin1_to_utf8_recursively($d);         return $ret;      } elseif (is_object($dat)) {         foreach ($dat as $i => $d) $dat->$i = self::convert_from_latin1_to_utf8_recursively($d);         return $dat;      } else {         return $dat;      }   }// Sample use// Just pass your array or string and the UTF8 encode will be fixed$data = convert_from_latin1_to_utf8_recursively($data);


I found the answer to this problem here

Just do

mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');


In my case I had a ucfirst on the asian letters string. This was not possible and produced a non utf8 string.