Home  >  Article  >  Backend Development  >  PHP pitfall records are continuously updated

PHP pitfall records are continuously updated

WBOY
WBOYOriginal
2016-08-08 09:19:271195browse

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教程有兴趣的朋友有所帮助。

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