urlencode("妞马")); $array = json_encode($array); echo ur"/> urlencode("妞马")); $array = json_encode($array); echo ur">

Home >Backend Development >PHP Tutorial >Convert Chinese characters to unicode php-About unicode transcoding

Convert Chinese characters to unicode php-About unicode transcoding

WBOY
WBOYOriginal
2016-07-28 08:30:181307browse

使用json_encode的时候,中文会被转码成为unicode格式,解决这个问题,主要有以下三种方法:

1.

<?php
echo json_encode("叨嘛",JSON_UNESCAPED_UNICODE);

2.
<?php
$array = array(&#39;doma&#39; => urlencode("叨嘛"));
$array = json_encode($array);
echo urldecode($array);

3.
<?php
$string = 'result: {"errorcode":"1","errormsg":"\u65b0\u589e\u5931\u8d25\uff01","result":""}';
function unicode($str, $encoding = null) {
	return preg_replace_callback ( '/\\\\u([0-9a-fA-F]{4})/u', create_function ( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "utf-8", "UTF-16BE");' ), $str );
}
echo unicode ( $string );

第三种方式是在读取的时候对unicode编码进行转码

不理解的地方:为什么正则表达式是这样写的?\\\\如何解释,无法匹配呀

                           回调函数

以上就介绍了汉字转unicode php-关于unicode的转码,包括了汉字转unicode方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn