【发布时间】:2021-02-21 01:38:09
【问题描述】:
我正在获取 JSON 数据以在我的网站中使用,我需要对其进行回显,如下所示:
<?php
$json = file_get_contents("/lang/translator.php"); // uses preg_replace to remove whitespace/newlines from my actual json file and then echo's it
$i18n = json_decode($json, true);
if (htmlentities($_GET['lang'], ENT_QUOTES) == 'en')
{
$arr = 'i18n_en';
}
else if (htmlentities($_GET['lang'], ENT_QUOTES) == 'ru')
{
$arr = 'i18n_ru';
}
?>
我是这样使用它的:
<?php echo $i18n[$arr]['string_key']; ?>
string_key 的值包含我网站的英语或俄语翻译,具体取决于它是哪个 JSON 数组。
问题:
当我上传包含西里尔字符(俄语)的 JSON 文件时,会发生这种情况:
хорошо --> хорошо
每个西里尔字符都会被转换为 HTML 实体。所以我发现我可以使用html_entity_decode() 来解决这个问题,但想象一下,为我的代码中的每个 单个<?php echo $i18n[$arr]['string_key']; ?> 调用执行此操作是多么耗时。没有办法解决吗?我尝试将$i18n 传递给html_entity_decode(),但它需要string,而不是array of strings。有什么想法吗?
我的 JSON 示例:
{
"i18n_en":
{
"key0": "value0",
"key1": "value1"
},
"i18n_ru":
{
"key0": "value0",
"key1": "value1"
}
}
【问题讨论】:
-
你为什么在
$_GET值上调用htmlentities?htmlentities应该只在页面中显示一个值并且你不希望它被呈现为 HTML 时使用。 -
我只是觉得这是防御黑客攻击的好习惯...也许我会删除这些调用。