Home  >  Article  >  Backend Development  >  Convert Unicode to UTF-8 using PHP

Convert Unicode to UTF-8 using PHP

WBOY
WBOYOriginal
2016-08-08 09:25:55945browse

function unescape($str) { $str = rawurldecode($str);

<code> preg_match_all(<span>"/(?:<span>%u</span>.{4})|&#x.{4};|&#\d+;|.+/U"</span>,<span>$str</span>,<span>$r</span>); <span>$ar</span> = <span>$r</span>[<span>0</span>]; <span>//print</span>_r(<span>$ar</span>); <span>foreach</span>(<span>$ar</span> as <span>$k</span>=><span>$v</span>) { <span>if</span>(<span>substr</span>(<span>$v</span>,<span>0</span>,<span>2</span>) == <span>"<span>%u</span>"</span>){ <span>$ar</span>[<span>$k</span>] = iconv(<span>"UCS-2BE"</span>,<span>"UTF-8"</span>,<span>pack</span>(<span>"H4"</span>,<span>substr</span>(<span>$v</span>,-<span>4</span>)));
  }
        elseif(<span>substr</span>(<span>$v</span>,<span>0</span>,<span>3</span>) == <span>"&#x"</span>){ <span>$ar</span>[<span>$k</span>] = iconv(<span>"UCS-2BE"</span>,<span>"UTF-8"</span>,<span>pack</span>(<span>"H4"</span>,<span>substr</span>(<span>$v</span>,<span>3</span>,-<span>1</span>)));
  }
        elseif(<span>substr</span>(<span>$v</span>,<span>0</span>,<span>2</span>) == <span>"&#"</span>) { <span>$ar</span>[<span>$k</span>] = iconv(<span>"UCS-2BE"</span>,<span>"UTF-8"</span>,<span>pack</span>(<span>"n"</span>,<span>substr</span>(<span>$v</span>,<span>2</span>,-<span>1</span>)));
        }
    } <span>return</span><span>join</span>(<span>""</span>,<span>$ar</span>);
}
echo unescape(<span>"紫星蓝"</span>);</code>

今天有用户反馈,表单系统用户提交的数据中文会乱码。测试发现问题出在 iconv 转换上。
iconv(‘UCS-2’, ‘GBK’, ‘中文’)
Google 搜索发现,原因是 Linux 服务器上 UCS-2 编码方式与 Winodws 不一致。
于是,我改成 iconv(‘UCS-2BE’, ‘GBK’, ‘中文’) 试试,中文正常了

以下是有关两个平台 UCS-2 编码的潜规则:

1, UCS-2 不等于 UTF-16。 UTF-16 每个字节使用 ASCII 字符范围编码,而 UCS-2 对每个字节的编码可以超出 ASCII 字符范围。UCS-2 和 UTF-16 对每个字符至多占两个字节,但是他们的编码是不一样的。

2, 对于 UCS-2, windows 下默认是 UCS-2LE。用 MultibyteToWidechar(或者A2W)生成的是 UCS-2LE 的 unicode。windows记事本可以将文本保存为 UCS-2BE,相当于多了层转换。

3, 对于 UCS-2, linux 下默认是 UCS-2BE。用iconv(指定UCS-2)来转换生成的是 UCS-2BE 的 unicode。如果转换windows平台过来的 UCS-2, 需要指定 UCS-2LE。

4, 鉴于windows和linux等多个平台对 UCS-2 的理解不同(UCS-2LE,UCS-2BE)。MS 主张 unicode 有个引导标志(UCS-2LE FFFE, UCS-2BE FEFF),以表明下面的字符是 unicode 并且判别 big-endian 或 little-endian。 所以从 windows 平台过来的数据发现有这个前缀,不用慌张。

5, linux 的编码输出,比如从文件输出,从 printf 输出,需要控制台做适当的编码匹配(如果编码不匹配,一般和该程序编译时的编码有若干关系),而控制台的转换输入需要查看当前的系统编码。比如控制台当前的编码是 UTF-8, 那么 UTF-8 编码的东西能正确显示,GBK 就不能;同样,当前编码是 GBK, 就能显示 GBK 编码,后来的系统应该更智能的处理好更多的转换了。不过通过 putty 等终端还是需要设置好终端的编码转换以解除乱码的烦恼。

PHP中对汉字进行UNICODE编码和解码的实现

<code><span>//将内容进行UNICODE编码</span><span><span>function</span><span>unicode_encode</span><span>(<span>$name</span>)</span> {</span><span>$name</span> = iconv(<span>'UTF-8'</span>, <span>'UCS-2'</span>, <span>$name</span>); <span>$len</span> = strlen(<span>$name</span>); <span>$str</span> = <span>''</span>; <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$len</span> - <span>1</span>; <span>$i</span> = <span>$i</span> + <span>2</span>)
    { <span>$c</span> = <span>$name</span>[<span>$i</span>]; <span>$c2</span> = <span>$name</span>[<span>$i</span> + <span>1</span>]; <span>if</span> (ord(<span>$c</span>) > <span>0</span>)
        { <span>// 两个字节的文字</span><span>$str</span> .= <span>'\u'</span>.base_convert(ord(<span>$c</span>), <span>10</span>, <span>16</span>).base_convert(ord(<span>$c2</span>), <span>10</span>, <span>16</span>);
        } <span>else</span> { <span>$str</span> .= <span>$c2</span>;
        }
    } <span>return</span><span>$str</span>;
} <span>$name</span> = <span>'MY,你大爷的'</span>; <span>$unicode_name</span>=unicode_encode(<span>$name</span>); <span>echo</span><span>'<h3>'</span>.<span>$unicode_name</span>.<span>'</h3>'</span>; <span>// 将UNICODE编码后的内容进行解码</span><span><span>function</span><span>unicode_decode</span><span>(<span>$name</span>)</span> {</span><span>// 转换编码,将Unicode编码转换成可以浏览的utf-8编码</span><span>$pattern</span> = <span>'/([\w]+)|(\\\u([\w]{4}))/i'</span>;
    preg_match_all(<span>$pattern</span>, <span>$name</span>, <span>$matches</span>); <span>if</span> (!<span>empty</span>(<span>$matches</span>))
    { <span>$name</span> = <span>''</span>; <span>for</span> (<span>$j</span> = <span>0</span>; <span>$j</span> < count(<span>$matches</span>[<span>0</span>]); <span>$j</span>++)
        { <span>$str</span> = <span>$matches</span>[<span>0</span>][<span>$j</span>]; <span>if</span> (strpos(<span>$str</span>, <span>'\\u'</span>) === <span>0</span>)
            { <span>$code</span> = base_convert(substr(<span>$str</span>, <span>2</span>, <span>2</span>), <span>16</span>, <span>10</span>); <span>$code2</span> = base_convert(substr(<span>$str</span>, <span>4</span>), <span>16</span>, <span>10</span>); <span>$c</span> = chr(<span>$code</span>).chr(<span>$code2</span>); <span>$c</span> = iconv(<span>'UCS-2'</span>, <span>'UTF-8'</span>, <span>$c</span>); <span>$name</span> .= <span>$c</span>;
            } <span>else</span> { <span>$name</span> .= <span>$str</span>;
            }
        }
    } <span>return</span><span>$name</span>;
} <span>echo</span><span>'MY,\u4f60\u5927\u7237\u7684 -> '.unicode_decode(<span>$unicode_name</span>);</code>

以上就介绍了用PHP将Unicode 转化为UTF-8,包括了方面的内容,希望对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