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 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错