dshun123456 2016-01-19 08:12
浏览 137
已采纳

如何将HTML数据转换为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.

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

[
    {
        "id": "99",
        "name": "Max-Gain on or before 25th January 2016 in Max India Limited.",
        "description": "

\tMax India Limited has announced the Record date for Three way De-Merger as 28th January 2016 (Thursday). <\/div>


\t <\/div>


\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>


\t <\/div>


\tIf any investor invests for a period Of 12 – 18 Months , this scrip will be a Multifold - Multi Bagger.<\/div>


\t <\/div>


\tTo View the full report on Max India Limited authored . <\/div>


\t <\/div>


\tPlease Click The Below Link<\/div>


\t


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


    },
  • 写回答

3条回答 默认 最新

  • douruanfan3030 2016-01-19 08:21
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式