任何人能帮助我吗?我怎么能拿UCS2 / HexEncoded字符

像 '你好' 将返回 “00480065006C006C006F”

这是HexEncoded值:

0048 = H 0065 = E 006C =升 006C =升 006F = O *

在阿拉伯另外(!مرحباعالم)将返回06450631062d0628064b06270020063906270644064500200021

我如何在PHP中获得编码UCS2?

有帮助吗?

解决方案

mb_convert_encoding($海峡,“UCS-2”,“汽车”)正常工作的字符串转换,但你必须做额外的工作来获得在浏览器中正确的输出。

您需要更改字符集的输出匹配UCS-2,以便能够使用回声以输出到一个页面。另外,可能需要通过的以设置内容类型标签在标题为好。

我已经包括在这里的三个例子在以下的unicode的变体:UCS-2,UTF-16,和UTF-8;因为它们不是所有的工作对我来说没有在Internet Explorer中调整。您可能需要你的PHP文件存储在UTF-8以获得正确的结果。另外,我在Windows上的英语版本,所以我不能在适当的RTL形式输入您的阿拉伯语的字符串。我很抱歉,如果你的字符串这里乱码。我向你保证,如果你在我的评论中所指出的位置替换它,你会得到正确的结果。最后,你可能会遇到麻烦在互联网Explorer的存在似乎有些古怪,当输出通过缓存重新加载观看UCS-2和UTF-16。不过,Firefox 3.5.5工作了所有的三种编码。如果你真的想让一个应用程序,我强烈建议你考虑使用UTF-8代替UCS-2。

UCS-2版

火狐3.5.5(OK,但Firefox说,这是UTF-16BE在我的测试。)点击 的Internet Explorer 7.0(也不行。没有检测/转换阿拉伯语正常。)

<?php
header('Content-Type: text/html; charset=UCS-2');
mb_http_output('UCS-2');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UCS-2" /></head><body>', 'UCS-2', 'auto');
echo mb_convert_encoding('encoding: ', 'UCS-2', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UCS-2', 'auto');
echo mb_convert_encoding('<br />', 'UCS-2', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UCS-2', 'auto')).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('</body>', 'UCS-2', 'auto');
?>

UTF-16版

火狐3.5.5(100%OK),点击 的Internet Explorer 7.0(失败。可能必须指定字节顺序。)

<?php
header('Content-Type: text/html; charset=UTF-16');
mb_http_output('UTF-16');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-16" /></head><body>', 'UTF-16', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-16', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-16', 'auto');
echo mb_convert_encoding('<br />', 'UTF-16', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-16', 'auto')).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('</body>', 'UTF-16', 'auto');
?>

UTF-8

火狐3.5.5(100%OK),点击 的Internet Explorer 7.0(100%OK)

<?php
header('Content-Type: text/html; charset=UTF-8');
mb_http_output('UTF-8');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>', 'UTF-8', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-8', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-8', 'auto');
echo mb_convert_encoding('<br />', 'UTF-8', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-8', 'auto')).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('</body>', 'UTF-8', 'auto');
?>
scroll top