Created
January 5, 2010 17:23
-
-
Save meklarian/269538 to your computer and use it in GitHub Desktop.
Reverse mapping of prior gist, Hex-string representation of UCS-2 into UTF-8 native string, with verified Arabic support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// REF: Source code corresponding to http://stackoverflow.com/questions/2005358/ucs2-hexencoded-characters-to-utf8-in-php | |
function local_hex2bin($h) | |
{ | |
if (!is_string($h)) return null; | |
$r=''; | |
for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); } | |
return $r; | |
}; | |
header('Content-Type: text/html; charset=UTF-8'); | |
mb_http_output('UTF-8'); | |
echo '<html><head>'; | |
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'; | |
echo '</head><body>'; | |
echo 'output encoding: '.mb_http_output().'<br />'; | |
$querystring = $_SERVER['QUERY_STRING']; | |
// NOTE: we could substitute one of the following: | |
// $querystring = '06450631062d0628064b06270020063906270644064500200021'; | |
// $querystring = '00480065006C006C006F'; | |
$ucs2string = local_hex2bin($querystring); | |
// NOTE: The source encoding could also be UTF-16 here. | |
// TODO: Should check byte-order-mark, if available, in case | |
// 16-bit-aligned bytes are reversed. | |
$utf8string = mb_convert_encoding($ucs2string, 'UTF-8', 'UCS-2'); | |
echo 'query string: '.$querystring.'<br />'; | |
echo 'converted string: '.$utf8string.'<br />'; | |
echo '</body>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment