【问题标题】:PHP | json_encode removes html tagsPHP | json_encode 移除 html 标签
【发布时间】:2017-06-11 15:46:47
【问题描述】:

我有一个大的多维数组,其中有多个字符串值,其中包含带有 html 标记和属性的字符串。

但是当我使用json_encode 时,我得到的输出有一些被删除的标签。

这是我在单独文件上尝试过的示例,以确定这是否真的是 json 编码的问题,结果证明我是对的。

<?php
$var = array(
    "type" => "<p style=\"text-align: center;\">sfds</p>"
);
   $encoded = json_encode($var);


echo '<pre>';
print_r($encoded);

遇到这种情况怎么办??

这是我从示例中得到的结果。

{"type":"
sfds<\/p>"}

这是我从多维数组中得到的结果。

{"data":[{"type":"columns","data":{"columns":[{"width":6,"blocks":[]},{"width":6,"blocks":[]}],"preset":"columns-6-6"}},{"type":"columns","data":{"columns":[{"width":6,"blocks":[{"type":"heading","data":{"text":"
I am the Heading<\/p>","mce_0":"

I am the Heading<\/p>"}},{"type":"heading","data":{"text":"

sfds<\/p>","mce_1":"

sfds<\/p>"}}]},{"width":6,"blocks":[{"type":"text","data":{"text":"

\n
Im Text<\/div>\n<\/div>","mce_2":"
\n
Im Text<\/div>\n<\/div>"}}]}],"preset":"columns-6-6"}},{"type":"text","data":{"text":"
\n
Im just a text<\/div>\n<\/div>","mce_3":"
\n
Im just a text<\/div>\n<\/div>"}}]} 

=-=-=-=-=-=-

更新:生成的我尝试编码的数组变量的 var_dump 的 HTML 源代码。

<pre> after set2_decode:<br>array(1) {
  ["data"]=&gt;
  array(3) {
    [0]=&gt;
    array(2) {
      ["type"]=&gt;
      string(7) "columns"
      ["data"]=&gt;
      array(2) {
        ["columns"]=&gt;
        array(2) {
          [0]=&gt;
          array(2) {
            ["width"]=&gt;
            int(6)
            ["blocks"]=&gt;
            array(0) {
            }
          }
          [1]=&gt;
          array(2) {
            ["width"]=&gt;
            int(6)
            ["blocks"]=&gt;
            array(0) {
            }
          }
        }
        ["preset"]=&gt;
        string(11) "columns-6-6"
      }
    }
    [1]=&gt;
    array(2) {
      ["type"]=&gt;
      string(7) "columns"
      ["data"]=&gt;
      array(2) {
        ["columns"]=&gt;
        array(2) {
          [0]=&gt;
          array(2) {
            ["width"]=&gt;
            int(6)
            ["blocks"]=&gt;
            array(2) {
              [0]=&gt;
              array(2) {
                ["type"]=&gt;
                string(7) "heading"
                ["data"]=&gt;
                array(2) {
                  ["text"]=&gt;
                  string(23) "<p>I am the Heading</p>"
                  ["mce_0"]=&gt;
                  string(23) "<p>I am the Heading</p>"
                }
              }
              [1]=&gt;
              array(2) {
                ["type"]=&gt;
                string(7) "heading"
                ["data"]=&gt;
                array(2) {
                  ["text"]=&gt;
                  string(39) "<p style="text-align: center;">sfds</p>"
                  ["mce_1"]=&gt;
                  string(39) "<p style="text-align: center;">sfds</p>"
                }
              }
            }
          }
          [1]=&gt;
          array(2) {
            ["width"]=&gt;
            int(6)
            ["blocks"]=&gt;
            array(1) {
              [0]=&gt;
              array(2) {
                ["type"]=&gt;
                string(4) "text"
                ["data"]=&gt;
                array(2) {
                  ["text"]=&gt;
                  string(59) "<div>
<div style="text-align: center;">Im Text</div>
</div>"
                  ["mce_2"]=&gt;
                  string(59) "<div>
<div style="text-align: center;">Im Text</div>
</div>"
                }
              }
            }
          }
        }
        ["preset"]=&gt;
        string(11) "columns-6-6"
      }
    }
    [2]=&gt;
    array(2) {
      ["type"]=&gt;
      string(4) "text"
      ["data"]=&gt;
      array(2) {
        ["text"]=&gt;
        string(65) "<div>
<div style="text-align: right;">Im just a text</div>
</div>"
        ["mce_3"]=&gt;
        string(65) "<div>
<div style="text-align: right;">Im just a text</div>
</div>"
      }
    }
  }
}
</pre>

【问题讨论】:

  • 而不是在浏览器中看到 - 打开生成的 html。或者在控制台中运行脚本。
  • @u_mulder 没明白你的意思。当编码的json剂量正常工作时,在浏览器中查看数据有什么用。我需要 json,但 json 应该包含带有属性的 html 标签。我已经在浏览器上试过了,数组工作正常,html标签不能直接看到,因为它在浏览器中查看时实现了。
  • 在浏览器中查看生成的 json 的源代码。
  • @u_mulder 先生,我已经完成了,我已经在我的问题中粘贴了报价。但这在我的 php 中怎么输出错误。??

标签: php html arrays json multidimensional-array


【解决方案1】:

您的浏览器隐藏了所有标签。 使用htmlentities()查看所有标签

print_r(htmlentities($encoded));

【讨论】:

  • htmlentities 适用于字符串或多维数组。所以我这样尝试。 ob_start(); print_r($set2_decode); $buffer = ob_get_clean(); echo "&lt;pre&gt; after set2_decode = ".htmlentities($buffer)."&lt;/pre&gt;";
【解决方案2】:

我认为 htmlentities 会有所帮助。请使用 print_r 然后转义你的 html。

echo htmlentities (print_r (json_encode($var), true));  

我希望这会有所帮助!

【讨论】:

    【解决方案3】:

    将 HTML 放在 JSON 响应中并不是最佳做法,最佳做法是在前端准确构建 html,然后调用已构建的 html 中的数据。但是请注意,您不能在 json 响应中使用纯 html。它通常被逃脱。所以你必须确保你的 html 是正确构建的。 这是一篇可以帮助您的好文章。

    https://www.thorntech.com/2012/07/4-things-you-must-do-when-putting-html-in-json/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多