'Json_Encode not returning html even i try to encode JSON_HEX_QUOT | 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);
?>
Solution 1:[1]
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"]))));
Solution 2:[2]
Exp:
$jsonData = json_encode($string, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
JSON_UNESCAPED_SLASHES = Allows you to use "//" characters.
JSON_PRETTY_PRINT = Writes the json data legibly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | XenoRo |
Solution 2 | Kuro Neko |