note 100272 modified in function.json-encode by danbrown

From: Date: Tue, 05 Oct 2010 15:46:32 +0000
Subject: note 100272 modified in function.json-encode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172978@lists.php.net to get a copy of this message
As json_encode() won't work with character sets other than UTF-8, this expression allows to encode strings for JSON regardless of the character set: <?php str_replace("\0", "\\u0000", addcslashes($string, "\t\r\n\"\\")); ?> You need to replace the nul character manually as addcslashes() won't do it right way. But BEWARE, this is only solution for common strings, other "unusual wild characters" like ESC, \b, \a etc. are not handled. --was-- As json_encode() won't work with character sets other than UTF-8, this expression allows to encode strings for JSON regardless of the character set: str_replace("\0", "\\u0000", addcslashes($string, "\t\r\n\"\\")); You need to replace the nul character manually as addcslashes() won't do it right way. But BEWARE, this is only solution for common strings, other "unusual wild characters" like ESC, \b, \a etc. are not handled. http://php.net/manual/en/function.json-encode.php

« previous php.notes (#172978) next »