简体   繁体   English

如何将HTML数据转换为json,php,mysql?

[英]How to convert HTML data to json, php, mysql?

how to convert HTML data to json, example as below, description content how to convert as json, it is from mysql, php., how to send json responce as plain text, but description comes from the mysql db as it is, but how to send the json responce api to androind. 如何将HTML数据转换为json,如下例所示,说明内容如何转换为json,它来自mysql,php。如何以纯文本格式发送json响应,但是描述来自mysql db,但如何发送json响应api到androind。

public function actionTestanalysis()
{
//echo $keyword=$_POST['keyword'];
    $query= Yii::app()->db->createCommand("select * from test ORDER BY id DESC")->queryAll();
    $arr = array();
    if(count($query) > 0) {

    foreach($query as $query){
    $arr[] = $query;    
    }
    }

    # JSON-encode the response
    $json_response = json_encode($arr);

    // # Return the response
    echo $json_response;


//exit;
}

json responce json响应

[
    {
        "id": "99",
        "name": "Max-Gain on or before 25th January 2016 in Max India Limited.",
        "description": "
\r\n\tMax India Limited has announced the Record date for Three way De-Merger as 28th January 2016 (Thursday). <\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tAnyone want to Gain from the three way De-Merger of Max India Group one should buy the shares of Max India Limited on or before – 25th January 2016 (Cum-Date – Monday Tentavily) otherwise to be on safer side you can buy on or before 22nd January 2016(Friday) and get invested in it.<\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tIf any investor invests for a period Of 12 – 18 Months , this scrip will be a Multifold - Multi Bagger.<\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tTo View the full report on Max India Limited authored . <\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tPlease Click The Below Link<\/div>\r\n
\r\n\t

\r\n\t\thttp:\/\/www.test.com\/index.php\/newsOpportunities\/list\/scroll\/no-pain-all-gain-maximum-benefit-in-max-india-ltd<\/a><\/p>\r\n<\/div>\r\n",
        "image": "",
        "status": "unlock"


    },

You have also one error in the cycle in your code. 您的代码循环中也有一个错误。

Try this: 尝试这个:

if (count($query) > 0) {
    foreach ($query as $queryElement) {
        $el = $queryElement;
        $el['description'] = trim(preg_replace('/\s+/', ' ', strip_tags($el['description'])));
        $arr[] = $el;
    }
}

Try before: echo $json_response set header content type to application/json 尝试之前:echo $ json_response将标头内容类型设置为application / json

<?PHP
header('Content-Type: application/json');
echo $json_response;

使用htmlentities()代替strip_tags() ,以保留存储在db中的实际内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM