Last active
August 29, 2015 13:59
-
-
Save gchacaltana/10690274 to your computer and use it in GitHub Desktop.
JSON Pretty Print in PHP 5.2
This file contains 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 characters
<?php | |
function jsonpp($json, $istr = ' ') | |
{ | |
$result = ''; | |
for($p=$q=$i=0; isset($json[$p]); $p++) | |
{ | |
$json[$p] == '"' && ($p>0?$json[$p-1]:'') != '\\' && $q=!$q; | |
if(strchr('}]', $json[$p]) && !$q && $i--) | |
{ | |
strchr('{[', $json[$p-1]) || $result .= "\n".str_repeat($istr, $i); | |
} | |
$result .= $json[$p]; | |
if(strchr(',{[', $json[$p]) && !$q) | |
{ | |
$i += strchr('{[', $json[$p])===FALSE?0:1; | |
strchr('}]', $json[$p+1]) || $result .= "\n".str_repeat($istr, $i); | |
} | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment