note 100272 deleted from function.json-encode by cmb
| From: | cmb@php.net | Date: | Tue, 05 Nov 2019 14:29:28 +0000 |
| Subject: | note 100272 deleted from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212928@lists.php.net to get a copy of this message | ||
Note Submitter: tomas at matfyz dot cz
----
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.