Json Encode神秘地将双引号添加到HTML

时间:2016-06-07 14:18:06

标签: php mysql json html5 codeigniter

我正在尝试将 mysql数据库中的数据导出为json文件。我创建了一个表单,允许我自定义要导出的字段。我正在使用 CodeIgniter 框架。

导出所有字段时,这是预期的 json输出

chapter_1: {
    chapter_id: 1
    chapter_no: 1
    chapter_scripture: '<html from ck editor>'
    chapter_total_verses: 33
    chapter_version_id: 1
    chapter_status_id: 1
    chapter_date_added: '2016-06-01'
    chapter_last_updated: '2016-06-01'
}

这里我试图将字段编码为json:

public function export_chapter($chapter_id)
{
    $chapter = $this->Chapter_model->get_by_id($chapter_id);
    $export_data = array();
    if($chapter)
    {
        if($this->input->post("fields_to_export"))
        {
            $chapter_array_key = "chapter_" . $chapter_id;
            $export_data = array(
                "chapter_" . $chapter_id => array()
            );
            foreach($this->input->post("fields_to_export") as $field_to_export)
            {
                $export_data[$chapter_array_key][$field_to_export] = $chapter[$field_to_export];
            }

            if(array_key_exists("chapter_scripture", $export_data[$chapter_array_key]))
            {
                $export_data[$chapter_array_key]["chapter_scripture"] =
                        "<div>" . $chapter["chapter_scripture"] . "</div>";
            }

            var_dump($export_data[$chapter_array_key]["chapter_scripture"]);
            var_dump(substr($export_data[$chapter_array_key]["chapter_scripture"], -20));
            $scripture_json = json_encode($export_data[$chapter_array_key]["chapter_scripture"]);
            var_dump($scripture_json);
            var_dump(substr($scripture_json, -20));
        }

        $data = array(
            "export_fields_data" => $this->_get_export_fields_data($chapter),
            "chapter" => $chapter,
        );

        $this->load->view("admin_panel/chapter/export_chapter_page", $data);
    }
    else
    {
        $this->session->set_userdata("message", "Bible Chapter doesn't exist.");
        redirect("admin_panel/chapter/browse_chapter");
    }
}

以下是var_dumps

的输出
C:\wamp64\www\proverbs_everyday\application\controllers\admin_panel\Chapter.php:341:string '<div><p><span style='font-size:18px;'><strong>1</strong></span>&nbsp;<span style='font-size:14px;'>The proverbs of Solomon the son of David, king of Israel;</span></p>

<p><sup>2&nbsp;</sup>To know wisdom and instruction; to perceive the words of understanding;</p>

<p><sup>3&nbsp;</sup>To receive the instruction of wisdom, justice, and judgment, and equity;</p>

<p><sup>4&nbsp;</sup>To give subtilty to the simple, to the young man knowledge and discretion.</p>

<p><sup>5&nbsp;</sup>A wise man will '... (length=3826)


C:\wamp64\www\proverbs_everyday\application\controllers\admin_panel\Chapter.php:342:string 'r of evil.</p></div>' (length=20)


C:\wamp64\www\proverbs_everyday\application\controllers\admin_panel\Chapter.php:344:string '"<div><p><span style='font-size:18px;'><strong>1<\/strong><\/span>&nbsp;<span style='font-size:14px;'>The proverbs of Solomon the son of David, king of Israel;<\/span><\/p>\r\n\r\n<p><sup>2&nbsp;<\/sup>To know wisdom and instruction; to perceive the words of understanding;<\/p>\r\n\r\n<p><sup>3&nbsp;<\/sup>To receive the instruction of wisdom, justice, and judgment, and equity;<\/p>\r\n\r\n<p><sup>4&nbsp;<\/sup>To give subtilty to the simple, to the young man knowledge and discretion.<\/p>\r\n\r\n<p><sup>5&'... (length=4025)


C:\wamp64\www\proverbs_everyday\application\controllers\admin_panel\Chapter.php:345:string 'f evil.<\/p><\/div>"' (length=20)

如您所见,chapter_scripture的开头和结尾都添加了双引号。知道为什么会这样吗?

来自数据库的示例chapter_scripture

<p><span style="font-size:18px;"><strong>1</strong></span>&nbsp;<span style="font-size:14px;">The proverbs of Solomon the son of David, king of Israel;</span></p>

<p><sup>2&nbsp;</sup>To know wisdom and instruction; to perceive the words of understanding;</p>

<p><sup>3&nbsp;</sup>To receive the instruction of wisdom, justice, and judgment, and equity;</p>

<p><sup>4&nbsp;</sup>To give subtilty to the simple, to the young man knowledge and discretion.</p>

<p><sup>5&nbsp;</sup>A wise man will hear, and will increase learning; and a man of understanding shall attain unto wise counsels:</p>

<p><sup>6&nbsp;</sup>To understand a proverb, and the interpretation; the words of the wise, and their dark sayings.</p>

<p><sup>7&nbsp;</sup>The fear of the&nbsp;Lord&nbsp;is the beginning of knowledge: but fools despise wisdom and instruction.</p>

<p><sup>8&nbsp;</sup>My son, hear the instruction of thy father, and forsake not the law of thy mother:</p>

<p><sup>9&nbsp;</sup>For they shall be an ornament of grace unto thy head, and chains about thy neck.</p>

<p><sup>10&nbsp;</sup>My son, if sinners entice thee, consent thou not.</p>

附加说明:

  • chapter_scripture包含来自CK Editor的HTML代码。
  • 现在我只是json编码chapter_scripture用于测试目的。但主要目的是对所有字段进行编码。

编辑

编辑1:

  • 已编辑的var转储。
  • 添加了示例chapter_scripture

0 个答案:

没有答案