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

标签 php mysql json

如何将HTML数据转换为json,示例如下,描述内容如何转换为json,它来自mysql,php。,如何以纯文本形式发送json响应,但描述来自mysql数据库,但是如何将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 响应

[
    {
        "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"


    },

最佳答案

您的代码循环中还存在一个错误。

试试这个:

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

关于php - 如何将HTML数据转换为json、php、mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871514/

相关文章:

mysql - 在 MYSQL 上将空值或 null 值的字段转换为 JSON 数据

Java ResultSet 内存不足,无法使用PreparedStatement 更新

python - 以递归方式评估 json 的优雅方法

php - 为什么我的 xmlhtpp 请求返回一个空数组?

php - 将德语单词存储到 MySql 数据库中时出现问题....?

mysql - 将 MySQL 时间戳列从一个时区转换为另一个时区

Java JSON 到数组

c# - 具有复杂类型参数的 WebInvoke 不适用于 Json 端点

php - Android - 使用 php 将图像上传到服务器的最佳方法

php - 合并 JSON 同时保持键布局