在json中转义html元素

时间:2015-01-05 09:18:37

标签: php json

我尝试将废弃的HTML内容转换为json。以下是我的无效输出:

{
    "item": {
        "title": "Take a look back at 2014’s biggest series A funding rounds across Asia (INFOGRAPHIC)",
        "desc": "Where's the money flowing for earlier stage startups? By narrowing down the playing field to A rounds, you can get an idea (hint: China and ecommerce).",
        "link": "https://www.example.com/asia-biggest-series-a-rounds-2014/",
        "content": "<p>2014 was a banner year for Asia’s startup ecosystem at large. The year saw some of the <a href=\\\"https://www.example.com/flipkart-wants-to-be-alibaba-with-1-billion-dollars/\\\">biggest tech investments</a> the region has ever seen, a <a href=\\\"https://www.example.com/alibaba-ipo-starts-93-dollars-per-share/\\\">blockbuster exit</a>, and the rise of <a href=\\\"https://www.example.com/rakuten-just-launched-a-100m-fund-for-startups-interview-saemin-ahn/\\\">Silicon Valley-level funds</a> by Asian investors.</p>\n<p>Every country in Asia seems to have a flagship startup that dominates the headlines, but it’s difficult to compare the ecosystem at large against its star player (or players). To even the playing field and get a better idea of where the investment money is flowing, we dug into the <em>Tech in Asia</em> archives to nail down each country’s biggest series A round in 2014. By narrowing the field to series A funding exclusively, you can get an idea of where younger startups are getting the most support and what verticals are attracting the most VC attention.</p>\n<p>Please note that the following data is for <em>disclosed</em> investments only. While some startups very well may have attracted larger sums, it’s left to pure speculation if the amount hasn’t been made public. If you’re aware of a larger A round for any of the nine countries listed &#8211; or for a country in Asia that didn’t make it on this list &#8211; please let us know in the comments section below.</p>\n<p><img src=\\\"https://www-techinasia.netdna-ssl.com/wp-content/uploads/2015/01/Biggest-Series-A-Funding-Rounds-Across-Asia-in-2014-3.png\\\" alt=\\\"Biggest-Series-A-Funding-Rounds-Across-Asia-in-2014-3\\\" width=\\\"700\\\" height=\\\"3888\\\" class=\\\"aligncenter size-full wp-image-213973\\\" /></p>\n<p><center><strong>See: <a href=\\\"https://www.example.com/crystal-ball-gazing-8-predictions-asias-startup-scene-2015/\\\">8 predictions for Asia’s startup scene in 2015</a></strong></center></p>\n<p>This post <a href=\\\"https://www.example.com/asia-biggest-series-a-rounds-2014/\\\" title=\\\"Take a look back at 2014&#8217;s biggest series A funding rounds across Asia (INFOGRAPHIC)\\\">Take a look back at 2014&#8217;s biggest series A funding rounds across Asia (INFOGRAPHIC)</a> appeared first on Tech in Asia.</p>"
    }
}

这是我尝试过的

$myArray["item"]["content"] = addcslashes($item_content,'"');
echo json_encode($myArray, true);

我也试过htmlentities()但是没有用。

1 个答案:

答案 0 :(得分:0)

你不想要addcslashes; json_encode将在必要时处理。此外,json_encode并未将true作为第二个参数。简单地:

$myArray["item"]["content"] = $item_content;
echo json_encode($myArray);

...或者如果要进行格式化,请将数字添加为json_encode的第二个参数。