REMOVE SPACES FROM JSON

the description

I was working on a project recently where I was having to strip out all unnecessary whitespace from JSON objects over and over. Instead of doing it manually, I figured there would be a tool out there to do it for me. Everything I found would also strip the spaces from the JSON values. Finding a good tool proved to be a taller order than expected.

So of course I made one.

This script loops through all characters in the given text, and strips out whitespaces including spaces, tabs, and newlines that are not inside of double quotes. Escaped quotes are accounted for. I made it with JSON in mind, but in theory it should work for any kind of text.

the implementation



the code

<?php

$string = htmlentities($_POST['the_json']);

// Add double quotes back in after htmlentities converted them.
$string = str_replace(array('&quot;', '&#34;'), '"', $string);

// Define our list of whitespace characters to strip.
$exclusions = array("\r\n", "\r", "\n", "\t", " ");

// Get ready...
$string_length = strlen($string);
$in_quotes = false;
$final_string_parts = array();

// Go!
for($i=0; $i<$string_length; $i++) {

	// If we're in quotes, or we're not in quotes and the current character
	// is not whitespace, then go ahead and include it in our final_string_parts array.
	if( $in_quotes || (!$in_quotes && !in_array($string[$i], $exclusions)) ) {
		$final_string_parts[] = $string[$i];
	}

	// If the current character is a double quote and it wasn't just escaped,
	// then toggle the in_quotes bool.
	if( $string[$i] == '"' && $string[$i-1] != '\\') {
		$in_quotes = !$in_quotes;
	}
}

// Put the string back together and echo it.
echo implode("", $final_string_parts);

?>