输出由PHP生成的JSON字符串中的HTML

Daa*_*aan 5 html php jquery json

我有一些输出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 ));
?>
Run Code Online (Sandbox Code Playgroud)

输出将如下:

{"titel":"Here comes the title","html":"<strong>Here is the HTML<\/strong>\n<br \/>\n<br \/>\n     And some more."}
Run Code Online (Sandbox Code Playgroud)

而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);
Run Code Online (Sandbox Code Playgroud)

HTML输出是:

<strong>Some HTML</strong> <br /> Some more text.
Run Code Online (Sandbox Code Playgroud)

所以它不会像HTML那样处理.

你能帮我吗?

Jit*_*esh 5

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

$html = htmlentities($html);从您的PHP文件中删除.

原因:htmlentities会转换

<strong>Some HTML</strong> <br /> Some more text.
Run Code Online (Sandbox Code Playgroud)

&lt;strong&gt;Some HTML&lt;/strong&gt; &lt;br /&gt; Some more text.
Run Code Online (Sandbox Code Playgroud)

当包含在html中时将显示:

<strong>Some HTML</strong> <br /> Some more text.
Run Code Online (Sandbox Code Playgroud)

  • 请试试这个.`<?php $ html = utf8_encode($ gegevens ['tekst']); echo json_encode(array('titel'=> $ gegevens ['titel'],'html'=> $ html)); ?>` (2认同)