Php 如何使用内嵌html解析json?

Php 如何使用内嵌html解析json?,php,json,node.js,Php,Json,Node.js,我在两个系统之间传递数据。在PHP中,我有一个设置文本格式的函数,如下所示: function cleanText($string) { return '<p>'.trim(preg_replace('/\n+/', '</p><p>', $string)).'</p>'; } {"content":"<p>Hej! Detta ska nu vara ordnat! Ledsen f\u00f6r besv\u00e4r.&

我在两个系统之间传递数据。在PHP中,我有一个设置文本格式的函数,如下所示:

function cleanText($string) {
    return '<p>'.trim(preg_replace('/\n+/', '</p><p>', $string)).'</p>';
}
{"content":"<p>Hej! Detta ska nu vara ordnat! Ledsen f\u00f6r besv\u00e4r.<\/p>"}
json = JSON.parse(json);
function cleanText($string) {
    return '<p>' . trim(preg_replace('/\n+/', '</p><p>', $string)) . '</p>';
}

$str = cleanText("My string");
echo json_encode(array("content" => $str));
Object {content: "<p>My string</p>"}
然后我用Ajax传递数据,因此我收到如下文本:

function cleanText($string) {
    return '<p>'.trim(preg_replace('/\n+/', '</p><p>', $string)).'</p>';
}
{"content":"<p>Hej! Detta ska nu vara ordnat! Ledsen f\u00f6r besv\u00e4r.<\/p>"}
json = JSON.parse(json);
function cleanText($string) {
    return '<p>' . trim(preg_replace('/\n+/', '</p><p>', $string)) . '</p>';
}

$str = cleanText("My string");
echo json_encode(array("content" => $str));
Object {content: "<p>My string</p>"}

但这将转义HTML,我不希望这样,我希望将字符串输出为HTML。我该怎么做?我应该用不同的方式在PHP中表示标记,还是可以用某种方式解析Ajax来保留HTML?

json\u encode
将数组转换为正确的json。我首先将其转换为数组,然后将其json_编码。您的PHP应该是这样的:

function cleanText($string) {
    return '<p>'.trim(preg_replace('/\n+/', '</p><p>', $string)).'</p>';
}
{"content":"<p>Hej! Detta ska nu vara ordnat! Ledsen f\u00f6r besv\u00e4r.<\/p>"}
json = JSON.parse(json);
function cleanText($string) {
    return '<p>' . trim(preg_replace('/\n+/', '</p><p>', $string)) . '</p>';
}

$str = cleanText("My string");
echo json_encode(array("content" => $str));
Object {content: "<p>My string</p>"}

听起来问题出在Node.js代码上,而不是PHP。要么是编码错误,要么是解码错误。通常你应该把你放进去的东西拿出来,不要逃跑。请展示你的PHP和更多的JS。我看不到大括号和方括号。您的JSON不正确。@CodeGodie它只是JSON的一部分。@Himmators您能在编码之前显示
$data
的内容吗?