note 102091 deleted from function.json-encode by crell

From: Date: Fri, 01 Jul 2022 19:25:56 +0000
Subject: note 102091 deleted from function.json-encode by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218835@lists.php.net to get a copy of this message
Note Submitter: bohwaz ---- This is intended to be a simple readable json encode function for PHP 5.3+ (and licensed under GNU/AGPLv3 or GPLv3 like you prefer): <?php function json_readable_encode($in, $indent = 0, $from_array = false) { $_myself = __FUNCTION__; $_escape = function ($str) { return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str); }; $out = ''; foreach ($in as $key=>$value) { $out .= str_repeat("\t", $indent + 1); $out .= "\"".$_escape((string)$key)."\": "; if (is_object($value) || is_array($value)) { $out .= "\n"; $out .= $_myself($value, $indent + 1); } elseif (is_bool($value)) { $out .= $value ? 'true' : 'false'; } elseif (is_null($value)) { $out .= 'null'; } elseif (is_string($value)) { $out .= "\"" . $_escape($value) ."\""; } else { $out .= $value; } $out .= ",\n"; } if (!empty($out)) { $out = substr($out, 0, -2); } $out = str_repeat("\t", $indent) . "{\n" . $out; $out .= "\n" . str_repeat("\t", $indent) . "}"; return $out; } ?>

« previous php.notes (#218835) next »