• <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 控制字符

        时间:2024-08-09
        • <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 控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP 允许的内存大小 下一篇:如何创建与我的 php 函数执行类似工作的程序(优化加速)

                  相关文章

                • <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'>

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