首頁 >後端開發 >php教程 >php踩坑記錄持續更新

php踩坑記錄持續更新

WBOY
WBOY原創
2016-08-08 09:19:271219瀏覽

1.命令行下将unicode转中文?

  • 方法1
<code><span><?php</span><span><span>function</span><span>unicodeDecode</span><span>(<span>$name</span>)</span>{</span><span>$json</span> = <span>'{"str":"'</span>.<span>$name</span>.<span>'"}'</span>;
  <span>$arr</span> = json_decode(<span>$json</span>,<span>true</span>);
  <span>if</span>(<span>empty</span>(<span>$arr</span>)) <span>return</span><span>''</span>; 
  <span>return</span><span>$arr</span>[<span>'str'</span>];
}
<span>$name</span> = <span>'\u65b0\u6d6a\u5fae\u535a'</span>;
<span>echo</span> unicodeDecode(<span>$name</span>); <span>//输出: 新浪微博 </span></code>
  • 方法2:
<code><span><span>function</span><span>replace_unicode_escape_sequence</span><span>(<span>$match</span>)</span> {</span><span>return</span> mb_convert_encoding(pack(<span>'H*'</span>, <span>$match</span>[<span>1</span>]), <span>'UTF-8'</span>, <span>'UCS-2BE'</span>);
}
<span>$name</span> = <span>'\u65b0\u6d6a\u5fae\u535a'</span>;
<span>$str</span> = preg_replace_callback(<span>'/\\\\u([0-9a-f]{4})/i'</span>, <span>'replace_unicode_escape_sequence'</span>, <span>$name</span>);
<span>echo</span><span>$str</span>; <span>//输出: 新浪微博 </span></code>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php踩坑记录持续更新,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn