<?php
function json_encode_ex($value) {
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$str = json_encode($value);
$str = preg_replace_callback("#\\\u([0-9a-f]{4})#i", "replace_unicode_escape_sequence", $str);
$str = stripslashes($str);
return $str;
} else {
return json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
}
function replace_unicode_escape_sequence($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
echo json_encode_ex("中文字!@#$%^&*()"); echo "\n"; echo json_encode("中文字!@#$%^&*()");
留言
張貼留言