PHP自定义函数格式化json数据示例
来源:互联网 发布:济南好玩的地方 知乎 编辑:程序博客网 时间:2024/05/21 17:03
<?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' => "脚本之家", '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 }}**/?>
0 0
- PHP自定义函数格式化json数据示例
- php JSON数据格式化方法
- PHP格式化输出json数据
- php JSON数据格式化方法 .
- php JSON数据格式化方法
- php JSON数据格式化方法
- php JSON数据格式化方法
- php JSON数据格式化方法
- php自定义的格式化时间示例代码
- [PHP]自定义格式化自符串函数
- PHP的serialize序列化数据与JSON格式化数据
- PHP的serialize序列化数据与JSON格式化数据
- PHP 防注入函数(格式化数据)
- 格式化JSON数据字符串
- 格式化json数据
- Json数据在线格式化
- json数据格式化
- 格式化输出json数据
- Java中使用BufferedReader读取文件内容
- openejb Example1
- SELinux 的启动、关闭与查看
- Android系统中的进程管理
- cpu 核心 线程 超频 超线程技术 等基本概念
- PHP自定义函数格式化json数据示例
- 用C++ 对中文进行UTF-8编解码
- 《Java并发编程》之线程中断与终止线程运行
- jQuery中的ajax方法参数详解
- 日志中出现大量Minact-Scn Master-Status: Grec-Scn Messages 信息
- MPAndroidChart是一款基于Android的开源图表库
- [00017]从字典中提取子集
- 从相册中选择多张图片
- android之ScrollView里嵌套ListView