English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This example explains how to format JSON data with a custom PHP function. Share it with everyone for reference, as follows:
<?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' => "Yell Tutorial", '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 } } **/ ?>
PS: Here are some practical json online tools for your reference and use:
Online JSON code verification, verification, beautification, and formatting tool:
http://tools.jb51.net/code/json
Online JSON formatting tool:
http://tools.jb51.net/code/jsonformat
Online XML/JSON conversion tool:
http://tools.jb51.net/code/xmljson
Online JSON code formatting/Beautify/Compress/Edit/Conversion tool:
http://tools.jb51.net/code/jsoncodeformat
C language style/HTML/CSS/JSON code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json
More about PHP-related content, readers who are interested can check the special topic of this site: 'PHP json format data operation skills summary', 'PHP XML file operation skills summary', 'PHP basic syntax tutorial', 'PHP array (Array) operation skills大全', 'PHP string (string) usage summary', 'php+MySQL Database Operation Tutorial and PHP Common Database Operation Skills Summary
I hope the content described in this article will be helpful to everyone's PHP program design.
Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)