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

  • <tfoot id='wfNdV'></tfoot>

    1. <small id='wfNdV'></small><noframes id='wfNdV'>

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

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

        编码/转义 JSON 控制字符

        Encoding/Escaping JSON Control Characters(编码/转义 JSON 控制字符)
        • <bdo id='HMl0h'></bdo><ul id='HMl0h'></ul>

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

            • <tfoot id='HMl0h'></tfoot>

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

                  本文介绍了编码/转义 JSON 控制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 MariaDB 的 COLUMN_JSON() 功能.正如 this bug 所示,该函数可以正确转义双引号,但不能转义其他字符被编码/转义.

                  I'm using MariaDB's COLUMN_JSON() function. As this bug illustrates, the function properly escapes double quotes, but not other characters that should be encoded/escaped.

                  这是一个愚蠢的示例查询,用于演示如何创建 JSON 列.

                  Here's a silly example query to demonstrate how the JSON column is created.

                  SELECT CONCAT('[', GROUP_CONCAT(COLUMN_JSON(COLUMN_CREATE(
                          'name', `name`,
                          'value', `value`
                      )) SEPARATOR ','), ']') AS `json`
                  FROM `settings`
                  

                  如果 namevalue 包含无效的 JSON 字符,json_decode 将失败.

                  If the name or value contain invalid JSON characters, json_decode will fail.

                  我编写了一个 PHP 函数来转义/编码来自查询的值,但似乎应该有更好的方法.

                  I've written a PHP function to escape/encode the value that comes from the query, but it seems like there should be a better way.

                  /**
                   * Makes sure the JSON values built by COLUMN_JSON() in MariaDB are safe for json_decode()
                   * Assumes that double quotes are already escaped
                   *
                   * @param string $mysql_json
                   * @return string
                   */
                  public static function jsonEscape($mysql_json)
                  {
                      $rtn = '';
                      for ($i = 0; $i < strlen($mysql_json); ++$i) {
                          $char = $mysql_json[$i];
                          if (($char === '\') && ($mysql_json[$i + 1] !== '"')) {
                              // escape a backslash, but leave escaped double quotes intact
                              $rtn .= '\\';
                          } elseif (($ord = ord($char)) && ($ord < 32)) {
                              // hex encode control characters (below ASCII 32)
                              $rtn .= '\u' . str_pad(dechex($ord), 4, '0', STR_PAD_LEFT);
                          } else {
                              $rtn .= $char;
                          }
                      }
                      return $rtn;
                  }
                  

                  像这样逐个字符地检查字符串效果不佳.也许有一个字符串替换或正则表达式会更高效?

                  Examine the string character-by-character like this doesn't perform well. Perhaps there's a string replacement or regular expression that would be more performant?

                  推荐答案

                  根据 Halcyon 的评论,我切换了str_replace() 解决方案,它的表现要好得多!trim(json_encode(13), '"')'\u' . str_pad(dechex(13), 4, '0', STR_PAD_LEFT) 稍微好一点,但它使意图更加清晰.

                  Based on a comment from Halcyon, I switched to a str_replace() solution, and it performs much better! The performance difference between trim(json_encode(13), '"') and '\u' . str_pad(dechex(13), 4, '0', STR_PAD_LEFT) is just barely better, but it makes the intent more clear.

                  private static $json_replace_search;
                  private static $json_replace_replace;
                  
                  /**
                   * Makes sure the JSON values built by GROUP_CONCAT() and COLUMN_JSON() in MariaDB are safe for json_decode()
                   * Assumes that double quotes are already escaped
                   *
                   * @param string $mysql_json
                   * @return string
                   */
                  public static function jsonEscape($mysql_json)
                  {
                      if (is_null(self::$json_replace_search)) {
                          // initialize
                          self::$json_replace_search = [];
                          self::$json_replace_replace = [];
                          // set up all of the control characters (below ASCII 32)
                          for ($i = 0; $i < 32; ++$i) {
                              self::$json_replace_search[$i] = chr($i);
                              self::$json_replace_replace[$i] = trim(json_encode(self::$json_replace_search[$i]), '"');
                          }
                      }
                      // replace them
                      return str_replace(self::$json_replace_search, self::$json_replace_replace, $mysql_json);
                  }
                  
                  /**
                   *
                   * @param string $mysql_json
                   * @return mixed
                   */
                  public static function jsonDecode($mysql_json)
                  {
                      return json_decode(self::jsonEscape($mysql_json));
                  }
                  

                  这篇关于编码/转义 JSON 控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <i id='IKuXq'><tr id='IKuXq'><dt id='IKuXq'><q id='IKuXq'><span id='IKuXq'><b id='IKuXq'><form id='IKuXq'><ins id='IKuXq'></ins><ul id='IKuXq'></ul><sub id='IKuXq'></sub></form><legend id='IKuXq'></legend><bdo id='IKuXq'><pre id='IKuXq'><center id='IKuXq'></center></pre></bdo></b><th id='IKuXq'></th></span></q></dt></tr></i><div id='IKuXq'><tfoot id='IKuXq'></tfoot><dl id='IKuXq'><fieldset id='IKuXq'></fieldset></dl></div>
                  • <small id='IKuXq'></small><noframes id='IKuXq'>

                        <tbody id='IKuXq'></tbody>

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

                          <tfoot id='IKuXq'></tfoot>
                          • <bdo id='IKuXq'></bdo><ul id='IKuXq'></ul>