UCS2/HexEncoded字符

mab*_*rim 3 php ucs2

任何人都可以帮助我吗?我怎么能得到UCS2/HexEncoded字符

比如'你好'会返回"00480065006C006C006F"

这是HexEncoded值:

0048 = H 0065 = e 006C = 006C = 1006F = o*

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

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

mek*_*ian 5

mb_convert_encoding($ str,'UCS-2','auto')可正常转换字符串,但您必须做额外的工作才能在浏览器中获得正确的输出.

您需要更改输出的字符集以匹配UCS-2,以便能够使用echo将其输出到页面.此外,您可能还需要通过标头中的标记设置Content-Type .

我在下面的unicode变体中包含了三个例子:UCS-2,UTF-16和UTF-8; 因为并非所有这些都在我没有调整Internet Explorer的情况下工作.您可能需要以UTF-8存储PHP文件才能获得正确的结果.此外,我在Windows的英文版本,所以我不能以适当的RTL格式输入您的阿拉伯字符串.如果你的字符串在这里乱码,我很抱歉.我向您保证,如果您在我的评论中注明的位置更换它,您将获得正确的结果.最后,您可能无法在Internet Explorer中查看UCS-2和UTF-16-当通过缓存重新加载输出时似乎有些奇怪.但是,FireFox 3.5.5适用于所有三种编码.如果您对制作应用程序非常认真,我强烈建议您考虑使用UTF-8而不是UCS-2.

UCS-2版本

FireFox 3.5.5(好吧,但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');
?>
Run Code Online (Sandbox Code Playgroud)

UTF-16版本

FireFox 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');
?>
Run Code Online (Sandbox Code Playgroud)

UTF-8

FireFox 3.5.5(100%Ok)
Internet Explorer 7.0(100%确定)

<?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');
?>
Run Code Online (Sandbox Code Playgroud)