function
unicode_encode(
$strLong
) {
$strArr
= preg_split('/(?<!^)(?!$)/u',
$strLong
);
$resUnicode
= '';
foreach
(
$strArr
as
$str
)
{
$bin_str
= '';
$arr
=
is_array
(
$str
) ?
$str
:
str_split
(
$str
);
foreach
(
$arr
as
$value
)
{
$bin_str
.=
decbin
(ord(
$value
));
}
$bin_str
= preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '
$1
$2
$3
',
$bin_str
);
$unicode
=
dechex
(
bindec
(
$bin_str
));
$_sup
= '';
for
(
$i
= 0;
$i
< 4 -
strlen
(
$unicode
);
$i
++)
{
$_sup
.= '0';
}
$str
= '\\u' .
$_sup
.
$unicode
;
$resUnicode
.=
$str
;
}
return
$resUnicode
;
}
function
unicode_decode(
$name
)
{
$pattern
= '/([\w]+)|(\\\u([\w]{4}))/i';
preg_match_all(
$pattern
,
$name
,
$matches
);
if
(!
empty
(
$matches
))
{
$name
= '';
for
(
$j
= 0;
$j
<
count
(
$matches
[0]);
$j
++)
{
$str
=
$matches
[0][
$j
];
if
(
strpos
(
$str
, '\\u') === 0)
{
$code
=
base_convert
(
substr
(
$str
, 2, 2), 16, 10);
$code2
=
base_convert
(
substr
(
$str
, 4), 16, 10);
$c
=
chr
(
$code
).
chr
(
$code2
);
$c
= iconv('UCS-2', 'UTF-8',
$c
);
$name
.=
$c
;
}
else
{
$name
.=
$str
;
}
}
}
return
$name
;
}
function
unicode_decode2(
$str
){
$json
= '{
"str"
:
"' . $str . '"
}';
$arr
= json_decode(
$json
, true);
if
(
empty
(
$arr
))
return
'';
return
$arr
['str'];
}
echo
unicode_encode('若水小站:qq963087326'),'<br>';
echo
unicode_decode('\u82e5\u6c34\u5c0f\u7ad9\u003a\u0071\u0071\u0039\u0036\u0033\u0030\u0038\u0037\u0033\u0032\u0036');