Home > Backend Development > PHP Tutorial > php把数据转换成汉字会丢失部分字符和数字的解决办法

php把数据转换成汉字会丢失部分字符和数字的解决办法

PHP中文网
Release: 2023-02-28 17:12:02
Original
1923 people have browsed it

php把数据转换成汉字会丢失部分字符和数字的解决办法

$code = 
xml
_to_
js
on($code);
Copy after login

用这个函数转换后,汉字等字符会变成下面这样子的

u6c5b\u671f\u5c3e\u58f0\u7684\u5230\u6765\uff0c\u6211\u7701\u5f00\u59cb\u8fdb\u5165\u6c34\u5229\u5efa\u8bbe\
u7684\u9ec4\u91d1\u65f6\u671f\u3002\u8bb0\u80058\u670826\u65e5\u83b7\u6089\uff0c\u4eca\u5e74\uff0c\u6211\
u7701\u5c06\u5b8c\u6210\u6c34\u5229\u6295\u8d44\u8fbe154\u4ebf\u5143\uff0c\u91cd\u70b9\u6db5\u76d616\u4e2a\
u65b9\u9762\u51713286\u4e2a\u9879\u76ee\u3002
Copy after login

回复讨论(解决方案)

1.可以试试这种:

$code = preg_replace_c
all
back('/\\\\u([0-9a-f]{4})/i', create_function( '$matches', '
return
 mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), $str);
Copy after login

2.

header('Content-type: text/html;charset=utf-8'); 
$code =<<< TXT \u6c5b\u671f\u5c3e\u58f0\u7684\u5230\u6765\uff0c\u6211\u7701\u5f00\u59cb\u8fdb\u5165\u6c34\
u5229\u5efa\u8bbe\u7684\u9ec4\u91d1\u65f6\u671f\u3002\u8bb0\u80058\u670826\u65e5\u83b7\u6089\uff0c\u4eca\
u5e74\uff0c\u6211\u7701\u5c06\u5b8c\u6210\u6c34\u5229\u6295\u8d44\u8fbe154\u4ebf\u5143\uff0c\u91cd\u70b9\
u6db5\u76d616\u4e2a\u65b9\u9762\u51713286\u4e2a\u9879\u76ee\u3002</P>\n\n TXT; 
echo preg_replace("#\\\u([0-9a-f]+)#ie", "iconv(&#39;UCS-2&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $code); 
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv(&#39;UCS-2&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $code);
Copy after login

汛期尾声的到来,我省开始进入水利建设的黄金时期。记者月日获悉,今年,我省将完成水利投资达亿元,重点涵盖个方面共个项目。


汛期尾声的到来,我省开始进入水利建设的黄金时期。记者8月26日获悉,今年,我省将完成水利投资达154亿元,重点涵盖16个方面共3286个项目。


丢失字符和数字的原因是你吞噬了多余的字符,unicode 只用4位十六进制表示

非常感谢版主,搞定了。。。版主是淫才啊。。。

$code = preg_replace_callback(&#39;/\\\\u([0-9a-f]{4})/i&#39;, 
create_function( &#39;$matches&#39;, &#39;return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");&#39;),
 $str);
Copy after login

以上就是php把数据转换成汉字会丢失部分字符和数字的解决办法的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template