Created
July 9, 2018 00:50
-
-
Save mdrakibhossainhawlader/243ebec30b86d8ab1df22d755357e10e to your computer and use it in GitHub Desktop.
Revisions
-
sepehr revised this gist
Jun 23, 2014 . 1 changed file with 13 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,6 +14,12 @@ function json_prettyprint($json) return json_encode($json, JSON_PRETTY_PRINT); } // Array passed? if (is_array($json)) { $json = json_encode($json); } $result = ''; $level = 0; $in_quotes = FALSE; @@ -36,25 +42,25 @@ function json_prettyprint($json) if ($in_escape) { $in_escape = false; } else if ($char === '"') { $in_quotes = !$in_quotes; } else if (! $in_quotes) { switch ($char) { case '}': case ']': $level--; $ends_line_level = NULL; $new_line_level = $level; break; case '{': case '[': $level++; @@ -66,9 +72,9 @@ function json_prettyprint($json) $post = " "; break; case " ": case "\t": case "\n": case "\r": $char = ""; $ends_line_level = $new_line_level; -
sepehr created this gist
Jun 23, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ <?php /** * Pretty print JSON objects. * * @param array $json JSON array to be printed. * * @return string */ function json_prettyprint($json) { if (defined("JSON_PRETTY_PRINT")) { return json_encode($json, JSON_PRETTY_PRINT); } $result = ''; $level = 0; $in_quotes = FALSE; $in_escape = FALSE; $ends_line_level = NULL; $json_length = strlen($json); for ($i = 0; $i < $json_length; $i++) { $post = ""; $new_line_level = NULL; $char = $json[$i]; if ($ends_line_level !== NULL) { $new_line_level = $ends_line_level; $ends_line_level = NULL; } if ($in_escape) { $in_escape = false; } else if ($char === '"') { $in_quotes = !$in_quotes; } else if (! $in_quotes) { switch ($char) { case '}': case ']': $level--; $ends_line_level = NULL; $new_line_level = $level; break; case '{': case '[': $level++; case ',': $ends_line_level = $level; break; case ':': $post = " "; break; case " ": case "\t": case "\n": case "\r": $char = ""; $ends_line_level = $new_line_level; $new_line_level = NULL; break; } // switch } // if else if ($char === '\\') { $in_escape = TRUE; } if ($new_line_level !== NULL) { $result .= "\n" . str_repeat("\t", $new_line_level); } $result .= $char . $post; } return $result; }