5

我有一些输出 json 的 PHP。

<?php
$html = utf8_encode($gegevens['tekst']);
$html = htmlentities($html);
//$html = htmlspecialchars($gegevens['tekst'], ENT_QUOTES, 'UTF-8');
echo json_encode(array( 'titel' => $gegevens['titel'], 'html' => $html ));
?>

输出将如下所示:

{"titel":"Here comes the title","html":"<strong>Here is the HTML<\/strong>\n<br \/>\n<br \/>\n     And some more."}

jQuery/Ajax 将是:

$.ajax({
                            type: "GET",
                            url: "content/popup.php?id=" + id2,
                            dataType: 'json',
                            crossDomain: true,
                            success: function(json) {
                            var titel = json['titel'];
                            var html = json['html'];


function ContentTonen()
{
                                // Div's legen van content
$('.popup_home_head_inside').empty();
$('.popup_home_content_inside').empty();

$('.popup_home_head_inside').html(titel);
var html2 = html.replace(/\"/g, "");
//$('.popup_home_content_inside').html(html2);
$('.popup_home_content_inside').html(html2);

HTML 输出为:

<strong>Some HTML</strong> <br /> Some more text.

所以它不会作为 HTML 处理。

你能帮我吗?

4

1 回答 1

5

您无需在服务器端使用 htmlentities 转义 html。

$html = htmlentities($html);从您的 php 文件中删除。

原因:htmlentities 会转换

<strong>Some HTML</strong> <br /> Some more text.

&lt;strong&gt;Some HTML&lt;/strong&gt; &lt;br /&gt; Some more text.

当包含在 html 中时将显示:

<strong>Some HTML</strong> <br /> Some more text.
于 2013-08-07T10:32:36.680 回答