• <tfoot id='RZ5N9'></tfoot>
  • <i id='RZ5N9'><tr id='RZ5N9'><dt id='RZ5N9'><q id='RZ5N9'><span id='RZ5N9'><b id='RZ5N9'><form id='RZ5N9'><ins id='RZ5N9'></ins><ul id='RZ5N9'></ul><sub id='RZ5N9'></sub></form><legend id='RZ5N9'></legend><bdo id='RZ5N9'><pre id='RZ5N9'><center id='RZ5N9'></center></pre></bdo></b><th id='RZ5N9'></th></span></q></dt></tr></i><div id='RZ5N9'><tfoot id='RZ5N9'></tfoot><dl id='RZ5N9'><fieldset id='RZ5N9'></fieldset></dl></div>

    <small id='RZ5N9'></small><noframes id='RZ5N9'>

      <bdo id='RZ5N9'></bdo><ul id='RZ5N9'></ul>

    <legend id='RZ5N9'><style id='RZ5N9'><dir id='RZ5N9'><q id='RZ5N9'></q></dir></style></legend>

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

        时间:2023-10-03

        <small id='pohCI'></small><noframes id='pohCI'>

            <tbody id='pohCI'></tbody>
        1. <legend id='pohCI'><style id='pohCI'><dir id='pohCI'><q id='pohCI'></q></dir></style></legend>

              <bdo id='pohCI'></bdo><ul id='pohCI'></ul>
              <i id='pohCI'><tr id='pohCI'><dt id='pohCI'><q id='pohCI'><span id='pohCI'><b id='pohCI'><form id='pohCI'><ins id='pohCI'></ins><ul id='pohCI'></ul><sub id='pohCI'></sub></form><legend id='pohCI'></legend><bdo id='pohCI'><pre id='pohCI'><center id='pohCI'></center></pre></bdo></b><th id='pohCI'></th></span></q></dt></tr></i><div id='pohCI'><tfoot id='pohCI'></tfoot><dl id='pohCI'><fieldset id='pohCI'></fieldset></dl></div>

            • <tfoot id='pohCI'></tfoot>
                  本文介绍了Laravel 中的“格式错误的 UTF-8 字符,可能编码不正确"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

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

                  以下声明

                  return JsonResponse::create($data); 
                  

                  返回以下错误

                  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());
                          }
                  

                  我变了:

                  return JsonResponse::create($data);
                  

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

                  但它仍然无法正常工作.

                  but it still isn't working.

                  我该如何解决?

                  推荐答案

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

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

                  这篇关于Laravel 中的“格式错误的 UTF-8 字符,可能编码不正确"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 UTF-8 编码的字符串上使用 str_split 下一篇:如何防止 json_encode() 删除包含无效字符的字符串

                  相关文章

                  最新文章

                  <small id='5RksQ'></small><noframes id='5RksQ'>

                  • <bdo id='5RksQ'></bdo><ul id='5RksQ'></ul>
                    <i id='5RksQ'><tr id='5RksQ'><dt id='5RksQ'><q id='5RksQ'><span id='5RksQ'><b id='5RksQ'><form id='5RksQ'><ins id='5RksQ'></ins><ul id='5RksQ'></ul><sub id='5RksQ'></sub></form><legend id='5RksQ'></legend><bdo id='5RksQ'><pre id='5RksQ'><center id='5RksQ'></center></pre></bdo></b><th id='5RksQ'></th></span></q></dt></tr></i><div id='5RksQ'><tfoot id='5RksQ'></tfoot><dl id='5RksQ'><fieldset id='5RksQ'></fieldset></dl></div>
                    1. <tfoot id='5RksQ'></tfoot>
                    2. <legend id='5RksQ'><style id='5RksQ'><dir id='5RksQ'><q id='5RksQ'></q></dir></style></legend>