This article is a PHP in the Unicode encoding and decoding of a detailed analysis of the introduction, the need for friends to refer to the
Method One:
Copy Code code as follows:
<?php
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];
$c 2 = $name [$i + 1];
if (ord ($c) > 0)
{///two bytes of text
$str. = ' u '. Base_convert (Ord ($c), (a). Str_pad (Base_convert (Ord ($c 2), a), 2, 0, str_pad_left);
}
Else
{
$str. = $c 2;
}
}
return $str;
}
//decoding of Unicode encoded content
function Unicode_decode ($name)
{
//conversion encoding to convert Unicode encoding to a browsable utf-8 encoding
$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);
$code 2 = Base_convert (substr ($STR, 4), 16, 10);
$c = Chr ($code). Chr ($code 2);
$c = iconv (' UCS-2 ', ' UTF-8 ', $c);
$name. = $c;
}
Else
{
$name. = $str;
}
}
}
return $name;
}
Method Two:
Copy Code code as follows:
Function Unicode2utf8 ($str) {
if (! $str) return $str;
$decode = Json_decode ($STR);
if ($decode) return $ Decode
$str = ' [']. $str. '"]';
$decode = Json_decode ($STR);
if (count ($decode) = = 1) {
return $decode [0];
}
return $str;
}