Skip to content

Instantly share code, notes, and snippets.

@gchacaltana
Last active August 29, 2015 13:59
Show Gist options
  • Save gchacaltana/10690274 to your computer and use it in GitHub Desktop.
Save gchacaltana/10690274 to your computer and use it in GitHub Desktop.
JSON Pretty Print in PHP 5.2
<?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