Skip to content

Instantly share code, notes, and snippets.

@jhuttner
Created June 8, 2011 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhuttner/1015474 to your computer and use it in GitHub Desktop.
Save jhuttner/1015474 to your computer and use it in GitHub Desktop.
public static function indentJSON($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
//$indentStr = ' ';
//$newLine = "\n";
$indentStr = '    ';
$newLine = "<br/>";
for($i = 0; $i <= $strLen; $i++) { $char = substr($json, $i, 1); if($char == '}' || $char == ']') { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } $result .= $char; if ($char == ',' || $char == '{' || $char == '[') { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } } return $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment