 <p>How about this:</p> <pre><code>function clean($text) { $parts = explode(' ', $text); foreach ($parts as $key =&gt; $value) $parts[$key] = preg_replace('/\s/', ' ', $value); return implode(' ', $parts); } </code></pre> <p>Indeed, if instead of cleaning the JSON file like this, you can use <a href="http://php.net/manual/en/function.json-encode.php" rel="nofollow">json_encode</a> to create it, you will get rid this problem in a previous step.</p>