note 80336 added to function.json-encode

From: Date: Thu, 10 Jan 2008 16:38:17 +0000
Subject: note 80336 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-135005@lists.php.net to get a copy of this message
Here's a quick function to pretty-print some JSON. Optimizations welcome, as this was a 10-minute dealie without efficiency in mind: // Pretty print some JSON // Takes a JSON string and returns it prettified // If the JSON is invalid, it will return false. function json_format($json) { $tab = " "; $new_json = ""; $indent_level = 0; $in_string = false; $json_obj = json_decode($json); if(!$json_obj) return false; $json = json_encode($json_obj); $len = strlen($json); for($c = 0; $c < $len; $c++) { $char = $json[$c]; switch($char) { case '{': case '[': if(!$in_string) { $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1); $indent_level++; } else { $new_json .= $char; } break; case '}': case ']': if(!$in_string) { $indent_level--; $new_json .= "\n" . str_repeat($tab, $indent_level) . $char; } else { $new_json .= $char; } break; case ',': if(!$in_string) { $new_json .= ",\n" . str_repeat($tab, $indent_level); } else { $new_json .= $char; } break; case ':': if(!$in_string) { $new_json .= ": "; } else { $new_json .= $char; } break; case '"': $in_string = !$in_string; default: $new_json .= $char; break; } } return $new_json; } ---- Server IP: 69.147.83.197 Probable Submitter: 38.98.105.130 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/80336 Del: integrated -- https://master.php.net/note/delete/80336/integrated Del: useless -- https://master.php.net/note/delete/80336/useless Del: bad code -- https://master.php.net/note/delete/80336/bad+code Del: spam -- https://master.php.net/note/delete/80336/spam Del: non-english -- https://master.php.net/note/delete/80336/non-english Del: in docs -- https://master.php.net/note/delete/80336/in+docs Del: other reasons-- https://master.php.net/note/delete/80336 Reject -- https://master.php.net/note/reject/80336 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135005) next »