国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP自定義函數格式化json數據示例

2024-05-04 23:49:51
字體:
來源:轉載
供稿:網友

本文實例講述了PHP自定義函數格式化json數據的方法。分享給大家供大家參考,具體如下:

<?php  /**   * Formats a JSON string for pretty printing   *   * @param string $json The JSON to make pretty   * @param bool $html Insert nonbreaking spaces and <br />s for tabs and linebreaks   * @return string The prettified output   */$arr = array("ret"=>0,"data"=>array('a' => 1, 'b' => "VeVb武林網", 'c' => 3, 'd' => 4, 'e' => 5));$json = json_encode($arr);function _format_json($json, $html = false) {    $tabcount = 0;    $result = '';    $inquote = false;    $ignorenext = false;    if ($html) {      $tab = "   ";      $newline = "<br/>";    } else {      $tab = "/t";      $newline = "/n";    }    for($i = 0; $i < strlen($json); $i++) {      $char = $json[$i];      if ($ignorenext) {        $result .= $char;        $ignorenext = false;      } else {        switch($char) {          case '{':            $tabcount++;            $result .= $char . $newline . str_repeat($tab, $tabcount);            break;          case '}':            $tabcount--;            $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char;            break;          case ',':            $result .= $char . $newline . str_repeat($tab, $tabcount);            break;          case '"':            $inquote = !$inquote;            $result .= $char;            break;          case '//':            if ($inquote) $ignorenext = true;            $result .= $char;            break;          default:            $result .= $char;        }      }    }    return $result;  }echo _format_json($json);/*{  "ret": 0,  "data": {    "a": 1,    "b": "/u811a/u672c/u4e4b/u5bb6",    "c": 3,    "d": 4,    "e": 5  }}**/?>

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 卓资县| 寻甸| 宜州市| 同德县| 漾濞| 张家港市| 合江县| 外汇| 长治市| 长治市| 衡水市| 虞城县| 弥勒县| 麻栗坡县| 山西省| 广丰县| 长宁县| 望奎县| 六枝特区| 阿拉善右旗| 苏尼特右旗| 罗城| 东源县| 阜新| 余干县| 清涧县| 昭苏县| 昌图县| 平度市| 浠水县| 自治县| 叙永县| 海南省| 雅安市| 上虞市| 广元市| 高安市| 上林县| 常德市| 名山县| 胶州市|