Indentation size with JSON_PRETTY_PRINT

The php function json_encode () has the ability to indent json output for a "pretty" version. This option is called: JSON_PRETTY_PRINT

A slight pet complaint is that this function uses 4 spaces in the specified indent.

Is it possible to use 2 spaces instead of 4 or an efficient way to process the output to reduce 4 spaces to 2 - without violating any of the json keys / values ​​that may have spaces in them.

+5
source share
1 answer

Try the following:

$data = ['some' => 'data']; $json = preg_replace_callback ('/^ +/m', function ($m) { return str_repeat (' ', strlen ($m[0]) / 2); }, json_encode ($data, JSON_PRETTY_PRINT)); 
+1
source

All Articles