Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions minify.json.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*! JSON.minify()
v0.1 (c) Kyle Simpson
v3.0 (c) Kyle Simpson
MIT License
*/

Expand All @@ -10,24 +10,25 @@ function json_minify($json) {
$in_string = false;
$in_multiline_comment = false;
$in_singleline_comment = false;
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $lc; $rc; $lastIndex = 0;
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $prevFrom = 0; $lc; $rc; $lastIndex = 0;

while (preg_match($tokenizer,$json,$tmp,PREG_OFFSET_CAPTURE,$lastIndex)) {
$tmp = $tmp[0];
$lastIndex = $tmp[1] + strlen($tmp[0]);
$lc = substr($json,0,$lastIndex - strlen($tmp[0]));
$prevFrom = $from;
$lc = substr($json,$prevFrom,$lastIndex - strlen($tmp[0]));
$rc = substr($json,$lastIndex);
if (!$in_multiline_comment && !$in_singleline_comment) {
$tmp2 = substr($lc,$from);
if (!$in_string) {
$tmp2 = preg_replace("/(\n|\r|\s)*/","",$tmp2);
$tmp2 = preg_replace("/(\n|\r|\s)+/","",$tmp2);
}
$new_str[] = $tmp2;
}
$from = $lastIndex;

if ($tmp[0] == "\"" && !$in_multiline_comment && !$in_singleline_comment) {
preg_match("/(\\\\)*$/",$lc,$tmp2);
preg_match("/(\\\\)+$/",$lc,$tmp2);
if (!$in_string || !$tmp2 || (strlen($tmp2[0]) % 2) == 0) { // start of string with ", or unescaped " character found to end string
$in_string = !$in_string;
}
Expand All @@ -54,4 +55,4 @@ function json_minify($json) {
return implode("",$new_str);
}

?>
?>