<?php
$str = ‘百度abc’;
$en = javaEn($str);
echo $en;
echo “<br>”;
echo javaDe($en);
function javaEn($str) {
$output = ”;
$str = iconv(‘GBK’, ‘UTF-16BE’, $str);
for($i = 0; $i < strlen($str); $i+=2) {
$code = ord($str{$i}) * 256 + ord($str{$i + 1});
$output .= ‘%u’.str_pad(dechex($code), 4, ’0′, STR_PAD_LEFT);
}
return $output;
}
function javaDe($str) {
preg_match_all(‘/%u0*([1-9a-e][0-9a-z]*)/’, $str, $arr);
$return = ”;
foreach($arr[1] as $v) {
if(strlen($v) > 2) {
$s = chr(hexdec(substr($v, 0, 2))).chr(hexdec(substr($v, 2)));
$return .= iconv(‘UTF-16BE’, ‘GBK’, $s);
} else {
$return .= chr(hexdec($v));
}
}
return $return;
}
?>
···
···