Code sharing of online conversion tools for php Unicode encoding and decoding
The code is as follows:
Function unicode_encode ($ name)
{
$ Name = iconv ('utf-8', 'ucs-2', $ name );
$ Len = strlen ($ name );
$ Str = '';
For ($ I = 0; $ I <$ len-1; $ I = $ I + 2)
{
$ C = $ name [$ I];
$ C2 = $ name [$ I + 1];
If (ord ($ c)> 0)
{// Two-byte text
$ Str. = '\ U '. base_convert (ord ($ c), 10, 16 ). str_pad (base_convert (ord ($ c2), 10, 16), 2, 0, STR_PAD_LEFT );
}
Else
{
$ Str. = $ c2;
}
}
Return $ str;
}
// Decodes the UNICODE encoded content
Function unicode_decode ($ name)
{
// Convert the Unicode encoding to the UTF-8 encoding that can be viewed
$ 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;
}