Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php UCS2/十六进制编码字符_Php_Ucs2 - Fatal编程技术网

Php UCS2/十六进制编码字符

Php UCS2/十六进制编码字符,php,ucs2,Php,Ucs2,有人能帮我吗?如何获取UCS2/HexEncoded字符 如“Hello”将返回“00480065006C006C006F” 这是十六进制编码的值: 0048=小时 0065=e 006C=l 006C=l 006F=o* 同样是阿拉伯语(!محب㶉㶉لم)将返回06450631062d0628064b06270020063906270644064500200021 如何在php中获得编码的UCS2?根据,多字节字符串模块(mbstring)支持UCS-2。启用此模块后,可以使用函数将字符串从一

有人能帮我吗?如何获取UCS2/HexEncoded字符

如“Hello”将返回“00480065006C006C006F”

这是十六进制编码的值:

0048=小时 0065=e 006C=l 006C=l 006F=o*

同样是阿拉伯语(!محب㶉㶉لم)将返回06450631062d0628064b06270020063906270644064500200021

如何在php中获得编码的UCS2?

根据,多字节字符串模块(mbstring)支持UCS-2。启用此模块后,可以使用函数将字符串从一种编码转换为另一种编码

引述:


mb\u convert\u编码($str,'UCS-2','auto')可以正确地转换字符串,但是您必须做额外的工作才能在浏览器中获得正确的输出

您需要更改输出的字符集以匹配UCS-2,以便能够使用echo将其输出到页面。此外,您可能还需要通过标题中的meta标记设置内容类型

我在下面的unicode变体中包括了三个示例:UCS-2、UTF-16和UTF-8;因为不是所有的都能为我工作,除非在Internet Explorer中进行调整。您可能需要将PHP文件存储在UTF-8中以获得正确的结果。另外,我使用的是英文版的Windows,因此无法以正确的RTL格式输入您的阿拉伯语字符串。很抱歉,如果您的字符串在这里被弄错了。我向您保证,如果您在我的评论中指出的位置更换它,您将得到正确的结果。最后,在internet explorer中查看UCS-2和UTF-16时可能会遇到问题-当通过缓存重新加载输出时,似乎会出现一些奇怪的情况。然而,Firefox3.5.5适用于所有三种编码。如果你真的想制作一个应用程序,我强烈建议你考虑使用UTF-8代替UCS-2。 UCS-2版本 FireFox 3.5.5(可以,但FireFox说我测试的是UTF-16BE。)
Internet Explorer 7.0(不正常。未正确检测/转换阿拉伯语。)


thx对于您的答案,我使用echo mb_convert_编码(“Hello”、“UCS-2”、“UTF-8”);但是它给我的输出是Hello而不是00480065006C006C006F,并且没有给我预期的输出任何建议或完整示例为了获得字符串底层字节的十六进制值,您应该调用unpack,请参见此答案中的示例:
string mb_convert_encoding  ( string $str  , string $to_encoding  [, mixed $from_encoding  ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding . 
<?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');
?>
<?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');
?>
<?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');
?>