문제
누구든지 나를 도울 수 있습니까? UCS2/Hexencoded 캐릭터를 어떻게 얻을 수 있습니까?
'hello'는 "00480065006c006c006f"를 반환합니다.
이것은 16 진분의 값입니다.
0048 = h 0065 = e 006c = l 006c = l 006f = o*
또한 아라비아 (! مرح 쩌 말했어 عالم)에서 06450631062D0628064B06270020063906270644064500200021을 반환합니다.
PHP에서 인코딩 된 UCS2를 어떻게 얻을 수 있습니까?
해결책
mb_convert_encoding ($ str, 'ucs-2', 'auto') 문자열을 변환하기 위해 올바르게 작동하지만 브라우저에서 올바른 출력을 얻으려면 추가 작업을 수행해야합니다.
사용하려면 UCS-2와 일치하도록 출력의 문자 세트를 변경해야합니다. 에코 페이지로 출력합니다. 또한 컨텐츠 유형을 메타 헤더에도 태그를 지정하십시오.
여기에는 다음 유니 코드 변이체에 세 가지 예를 포함 시켰습니다 : UCS-2, UTF-16 및 UTF-8; 그들 모두가 인터넷 익스플로러를 조정하지 않고 나를 위해 일한 것은 아닙니다. 적절한 결과를 얻으려면 PHP 파일을 UTF-8에 저장해야 할 수도 있습니다. 또한 영어 버전의 Windows에 있으므로 적절한 RTL 형식으로 아랍어 문자열을 입력 할 수 없습니다. 당신의 끈이 여기에 얽혀 있으면 죄송합니다. 내 의견으로 언급 된 위치에서 교체하면 적절한 결과를 얻을 수 있다고 확신합니다. 마지막으로, 인터넷 익스플로러에서 UCS-2 및 UTF-16을 보는 데 어려움이있을 수 있습니다. 캐시를 통해 출력이 다시로드 될 때 몇 가지 이상한 점이있는 것 같습니다. 그러나 Firefox 3.5.5는 세 가지 인코딩 모두에서 일했습니다. 앱을 만드는 것이 진지하다면 UCS-2 대신 UTF-8을 사용하는 것이 좋습니다.
UCS-2 버전
Firefox 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 버전
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');
?>
UTF-8
Firefox 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');
?>
다른 팁
에 따르면 이 웹 페이지, MBSTRING (Multi-Byte String Module)는 UCS-2를 지원합니다. 이 모듈을 활성화 한 후에는 기능을 사용할 수 있습니다. mb_convert_encoding 하나의 인코딩에서 다른 인코딩에서 문자열을 변환합니다.
인용 mb_convert_encoding 함수의 문서화:
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 .