2016-08-22 39 views
-1

我想從PHP返回HTML。我發現了一個來自StackOverflow的解決方案,它在json_encode之後插入JSON_HEX_QUOT | JSON_HEX_TAG,但它不適用於我。如果我只是立即迴應它,它正在工作並在頁面上顯示文本,但它不適用於json_encodeJson_Encode不返回HTML,即使我嘗試編碼JSON_HEX_QUOT | JSON_HEX_TAG

這裏是我的代碼:

<?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); 
?> 
+0

我不知道你的問題是什麼。 'json_encode'應該返回JSON字符串而不是HTML。 'json_encode'沒有返回任何東西?用['json_last_error']檢查錯誤(http://php.net/manual/en/function.json-last-error.php)。 –

+1

如果你想返回HTML,你爲什麼使用'json_encode'?你甚至知道你想要什麼? –

+0

其實他們是在數據庫中的HTML,我想刪除這些標籤並返回JSON格式。但問題是當我從數據庫中獲取數據時,如果wirte echo $ snippetData。它的工作和顯示,但它現在使用json_encode。但如果我複製該字段並將其分配給變量,它也可以像 一樣工作$ html =「」; 如果我從數據庫複製字段並將其存儲在變量中,那麼我寫json_encode(array(「data」=> $ html));那麼它的工作就不知道該怎麼做 –

回答

1

我想實現HTML沒有標籤並返回結果以JSON格式。所以我這樣做了:

echo json_encode(array("id"=>"1","data" => utf8_encode(strip_tags($row["post_content"])))); 
相關問題