Home > Article > Backend Development > PHP utf-8 to unicode function page 1/2_PHP tutorial
UTF encoding
UTF-8 encodes UCS in 8-bit units. The encoding method from UCS-2 to UTF-8 is as follows:
UCS-2 encoding (hexadecimal)
UTF-8 byte stream (binary)
0000 - 007F
0xxxxxxx
0080 - 07FF
110xxxxx 10xxxxxx
0800 - FFFF
1110xxxx 10xxxxxx 10xxxxxx
For example, the Unicode encoding of the character "汉" is 6C49. 6C49 is between 0800-FFFF, so you must use a 3-byte template Got: 1110xxxx 10xxxxxx 10xxxxxx. Writing 6C49 in binary is: 0110 110001 001001. Using this bit stream to replace x in the template in turn, we get: 11100110 10110001 10001001, which is E6 B1 89.
Finally, the conversion between unicode and utf8 is completed.
If the utf-8 encoded character ch is 3 bytes. xx yy zz
AND the xx and 1F to get a
AND the yy and 7F to get b
AND the zz and 7F to get c
(64a+b)*64+c = ch (unicode encoding)
echo.php is nothing. Just a few functions.
");
//Write unicode file
$ucs2data = utf8ToUnicode($data,"little");
$endian = chr(0xFE).chr(0xFF);
$endian = chr(0xFF).chr(0xFE);
$rt = file_put_contents ( "ucs2.txt", $endian.$ucs2data);
//19:32, utf8toUnicode function ok.
//20:09. Found the problem of little endian and big endian.
//The unicode string stored in big endian cannot be recognized by
//only notepad. 🎜>$rt = file_put_contents ( "usc2ys_data.txt", $ucs2_ysdata);
//Write utf8 file
$utf8data = unicodeToUtf8($ucs2data); // 20:52. Convert the string back to utf8 Code ok.
$rt = file_put_contents ( "utf8.txt", $utf8data);
echo(urlencode($utf8data));echo("");
$esc = utf8Escape($data) ;
echot($esc);
$esc = phpEscape($data);
echot($esc);
$unesc = phpUnescape($esc);
echot($unesc );
/**
* This function converts a utf8 encoded string into a unicode encoded string
* Parameter str, a utf8 encoded string.
* Parameter order, storage data format, whether big endian or little endian, the default unicode storage order is little.
* For example: the unicode code of "big" is 5927. Storage in little mode is: 27 59. In big mode, the order remains unchanged: 59 27.
* Little format files must have FF FE at the beginning. Files stored in big format start with FE FF. otherwise. There will be serious confusion.
* This function only converts characters and is not responsible for adding headers.
* The string converted by iconv is stored in big endian.
* Returns ucs2string, the converted string.
* Thanks for nagging (xuzuning)
*/
function utf8ToUnicode($str,$order="little")
{
$ucs2string ="";
$n=strlen( $str);
for ($i=0;$i0x80) { //110xxxxx 10xxxxxx
$a = (ord($str[$i]) & 0x3F )0x80 && ord($str[$i +2])>0x80) { //1110xxxx 10xxxxxx 10xxxxxx
$a = (ord($str[$i]) & 0x1F) Convert to utf8 encoded string
* Parameter str, unicode encoded character string.
* Parameter order, the storage order of unicode strings, whether it is big endian or little endian.
* Returns utf8string, the converted string.
*
*/
function unicodeToUtf8($str,$order="little")
{
$utf8string ="";
$n=strlen($str);
for ($i=0;$i is converted back.
$i++; //Two bytes represent a unicode character.
$c = "";
if($val utf8string . = $c;
}
return $utf8string;
} // end func
/*
* Encode the utf8-encoded string into unicode pattern, which is equivalent to escape
* The reason why we only accept utf8 codes is because there is only formula conversion between utf8 codes and unicode. Other codes must be converted by looking up the code table.
* I don’t know if the regular rules for finding utf8 codes are completely correct.
* Although calling utf2ucs to calculate the code value of each character is too inefficient. However, the code is not easy to read if the calculation process is embedded
*/ <.>function utf8Escape($str) {
preg_match_all("/[\xC0-\xE0].|[\xE0-\xF0]..|[\x01-\x7f]+/",$str,$ r);
//prt($r);
$ar = $r[0];
foreach($ar as $k=>$v) {
$ord = ord ($v[0]);
if( $ordutf8 code
$ar[$k] = "%u".utf2ucs($v);
}
elseif ($ordutf8 code
$ar[$k] = "%u".utf2ucs($v);
}
}//foreach
return join("",$ar);
}
/**
*
* Convert utf8 encoded characters to ucs-2 encoding
* Parameter utf8 encoded characters.
* Return the unicode code value of the character. You know the code value. You can use chr to get the characters out.
*
* Principle: The algorithm for converting unicode to utf-8 code is.
The reverse algorithm of this process is this function. Head fixed position reversed and.
*/
function utf2ucs($str){
$n=strlen($str);
if ($n=3) {
$highCode = ord($str[0 ]);
$midCode = ord($str[1]);
$lowCode = ord($str[2]);
$a = 0x1F & $highCode;
$b = 0x7F & $midCode;
$c = 0x7F & $lowCode;
$ucsCode = (64*$a + $b)*64 + $c;
}
elseif ($n== 2) {
$highCode = ord($str[0]);
$lowCode = ord($str[1]);
$a = 0x3F & $highCode; //0x3F is 0xC0 Complement
$b = 0x7F & $lowCode; //0x7F is the complement of 0x80
$ucsCode = 64*$a + $b;
}
elseif($n==1) {
$ucscode = ord($str);
}
return dechex($ucsCode);
}
/*
* Usage: This function is used to reverse Characters encoded by the escape function of javascript.
* I don’t know if there is any problem with the key regular search.
* Parameter: JavaScript encoded string.
* For example: unicodeToUtf8("%u5927")= big
* 2005-12-10
*
*/
function phpUnescape($escstr){
preg_match_all("/ %u[0-9A-Za-z]{4}|%.{2}|[0-9a-zA-Z.+-_]+/",$escstr,$matches); //prt($ matches);
$ar = &$matches[0];
$c = "";
foreach($ar as $val){
if (substr($val,0,1 )!="%") { //If it is the ascii code of alphanumeric +-_.
$c .=$val;
}
elseif (substr($val,1,1)! ="u") { //If it is an ascii code of non-alphanumeric +-_.
$x = hexdec(substr($val,1,2));
$c .=chr($x) ;
}
else { //If it is a code greater than 0xFF
$val = intval(substr($val,2),16);
if($val %u".bin2hex( iconv( 'gbk' ,"UCS-2",$chars[$i].$chars[$i+1] ) );
$i++;
}
}//foreach
return $ar;
}
?>