doudi1449 2015-01-05 09:18
浏览 125

在json中转义html元素

I try to put scrapped html content and turn it into json. Below is my invalid output:

{
    "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>
<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>
<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>
<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>
<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>
<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>"
    }
}

This is what I've tried

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

I also tried htmlentities() but doesn't work.

  • 写回答

1条回答 默认 最新

  • duanou8504 2015-01-05 09:21
    关注

    You don't want addcslashes; json_encode will handle that where necessary. Also, json_encode doesn't take true as a second argument. Simply:

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

    ...or add a number as a second argument to json_encode if you want formatting.

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制