donglunzai4288 2016-08-22 18:30
浏览 298
已采纳

即使我尝试编码JSON_HEX_QUOT |,Json_Encode也不返回html JSON_HEX_TAG

I want to return HTML from PHP. I found a solution from StackOverflow which is insert JSON_HEX_QUOT | JSON_HEX_TAG after array in json_encode but it does not work for me. If I simply echo it right away it's working and displays the text on page but it doesn't work with json_encode.

Here is my code:

<?php 
    header('Content-Type: application/json');
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "mcblog";
    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql = "select ID,post_parent,post_title,post_content,post_date from wp_posts where post_content<>'' and post_title<>'' order by post_date desc limit 0,10";
    $result = $conn->query($sql);
    $data=array();
    $row = $result->fetch_assoc();  
    $html=strip_tags($row["post_content"]);
    $snippetData=array('status'=>'1','data'=>$row["post_content"]);
    echo json_encode($snippetData, JSON_HEX_QUOT | JSON_HEX_TAG);
?>
  • 写回答

1条回答 默认 最新

  • douxiong4892 2016-08-23 17:36
    关注

    I wanted to achieve HTML without tags and return that result in JSON format. So I done it with this:

    echo json_encode(array("id"=>"1","data" => utf8_encode(strip_tags($row["post_content"]))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献