Stuff Here 这是无效的,因为当它被编码-6ren">
gpt4 book ai didi

php - 将 Json 字符串放入 html 标签的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 13:19:38 24 4
gpt4 key购买 nike

将 json 字符串(编码的 php 数组)放入 html 标记的最佳方法是什么?

<?php
$content = array(key1=>'value 1', key2=>'value 2');
?>
<span class="someclass" data="<?php echo(json_encode($content)); ?>">Stuff Here</span>

这是无效的,因为当它被编码时,它将包含破坏 html 的语音标记。

如果我使用 php 函数 htmlentities 就足够了吗?还是会有一些字符会破坏它?

谢谢。

最佳答案

您可以使用 base64 编码,它可以很容易地被您希望使用的每种语言(如 javascript)解码:

<span class="someclass" data="<?php echo base64_encode (json_encode ($content)); ?>">Stuff Here</span>

它只使用 a-z、A-Z 和 0-9。

关于php - 将 Json 字符串放入 html 标签的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com