Source Code:
(back to article)
<?php // Define an array of data to encode as JSON $data = [ 'name' => 'John Doe', 'age' => 30, 'email' => 'john.doe@example.com', 'favorite_quotes' => [ "We can't solve problems by using the same kind of thinking we used when we created them. - Albert Einstein", "The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela", ], ]; // Encode the array as a JSON string, with Unicode escape sequences replaced $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $json = preg_replace_callback( '/\\\\u([a-f0-9]{4})/i', function ($match) { return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); }, $json ); // Output the resulting JSON string echo $json;
Result:
Report an issue