php Laravel 中的“格式错误的 UTF-8 字符,可能编码不正确”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31115982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:15:46  来源:igfitidea点击:

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

phpjsonlaravel-4utf-8

提问by Dzung Nguyen

I'm using Laravel (a PHP framework) to write a service for mobile and have the data returned in JSONformat. In the data result there are some fields encoded in UTF-8.

我正在使用 Laravel(一个 PHP 框架)为移动设备编写服务并以JSON格式返回数据。在数据结果中有一些字段以UTF-8.

The following statement

以下声明

return JsonResponse::create($data); 

returns the error below

返回以下错误

InvalidArgumentException
HELP
Malformed UTF-8 characters, possibly incorrectly encoded

Open: /var/www/html/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/JsonResponse.php
        } catch (\Exception $exception) {
            restore_error_handler();

            throw $exception;
        }

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new \InvalidArgumentException($this->transformJsonError());
        }

I've changed:

我变了:

return JsonResponse::create($data);

to

return JsonResponse::create($data, 200, array('Content-Type'=>'application/json; charset=utf-8' ));

but it still isn't working.

但它仍然无法正常工作。

How can I fix it?

我该如何解决?

回答by Tiago Gouvêa

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

我写了这个方法来处理 UTF8 数组和 JSON 问题。它适用于数组(简单和多维)。

/**
 * 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);

回答by Leo Leoncio

I found the answer to this problem here

我在这里找到了这个问题的答案

Just do

做就是了

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

回答by alex

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

就我而言,我ucfirst在亚洲字母字符串上有一个。这是不可能的,并产生了一个非 utf8 字符串。

回答by М.Б.

In my case, this causes error:

就我而言,这会导致错误:

return response->json(["message" => "Model status successfully updated!", "data" => $model], 200);

but this not:

但这不是:

return response->json(["message" => "Model status successfully updated!", "data" => $model->toJson()], 200);

回答by Wal Heredia

I've experienced the same problem. The thing is that I forgot to start the apache and mysql in xampp... :S

我也遇到过同样的问题。问题是我忘记在 xampp 中启动 apache 和 mysql ... :S

回答by SNS - Web et Informatique

For more solution i have completed the solution of (the great) Tiago Gouvêa exposed before only for strings and arrays

对于更多解决方案,我已经完成了之前仅针对字符串和数组公开的(伟大的)Tiago Gouvêa 的解决方案

i used md_convert_encoding() function instead of utf8_encode(), it works for me : (12 hours loosed ...)

我使用了 md_convert_encoding() 函数而不是 utf8_encode(),它对我有用:(12 小时松散......)

// this object return me a big array who have multiple arrays imbricated

// 这个对象返回一个包含多个数组的大数组

$get_days = program::get_days($ARR, $client);

// and i use this function for well parsing what the server return *

// 我使用这个函数来很好地解析服务器返回的内容 *

                function convert_to_utf8_recursively($dat){
                      if( is_string($dat) ){
                         return mb_convert_encoding($dat, 'UTF-8', 'UTF-8');
                      }
                      elseif( is_array($dat) ){
                         $ret = [];
                         foreach($dat as $i => $d){
                           $ret[$i] = convert_to_utf8_recursively($d);
                         }
                         return $ret;
                      }
                      else{
                         return $dat;
                      }
                }

                // use 
                $data = convert_to_utf8_recursively($get_days);
  • what the server return * i talk to that because i test the same code in two different servers the first return me a well formatted json , without any function, as we usually do BUT the second server does not send me back anything if I do not apply this function ...
  • 服务器返回什么 * 我会这么说,因为我在两个不同的服务器上测试了相同的代码,第一个向我返回一个格式良好的 json ,没有任何功能,就像我们通常做的那样,但是如果我不这样做,第二个服务器不会向我发送任何东西应用这个功能...

回答by Tikam Chand

Set the charset at after you made the connection to db like

在连接到 db 后将字符集设置为

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (!$conn->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $conn->error);
    exit();
} else {
    printf("Current character set: %s\n", $conn->character_set_name());
}

回答by Bora

I got this error and i fixed the issue with iconvfunction like following:

我收到了这个错误,我用iconv如下函数解决了这个问题:

iconv('latin5', 'utf-8', $data['index']);

回答by ThiagoYou

I know it's already an old question, but i had the same error today. For me setting the connection variable on model did the work.

我知道这已经是一个老问题了,但我今天遇到了同样的错误。对我来说,在模型上设置连接变量完成了工作。

/**
 * Table properties
 */
protected $connection = 'mysql-utf8';
protected $table = 'notification';
protected $primaryKey = 'id';

I don't know if the issue was with the database (probably), but the texts fields with special chars (like ~, ′ e etc) were all messed up.

我不知道问题是否与数据库有关(可能),但是带有特殊字符(如 ~、' e 等)的文本字段都被搞砸了。

---- Editing

---- 编辑

That $connection var is used to select wich db connection your model will use. Sometimes it happens that in database.php (under /config folder) you have multiples connections and the default one is not using UTF-8 charset.

该 $connection var 用于选择您的模型将使用的 db 连接。有时会发生在 database.php(在 /config 文件夹下)中有多个连接并且默认连接不使用 UTF-8 字符集。

In any case, be sure to properly use charset and collation into your connection.

在任何情况下,请确保在您的连接中正确使用字符集和排序规则。

'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'your_database'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'database_password'),
        'unix_socket' => env('DB_SOCKET', ''),
        'prefix' => '',
        'strict' => false,
        'engine' => null
    ],

    'mysql-utf8' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'your_database'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'database_password'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null
    ],