Home Backend Development PHP Tutorial Unicode encoding converter php utf-8 to unicode function page 1/2

Unicode encoding converter php utf-8 to unicode function page 1/2

Jul 29, 2016 am 08:38 AM

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 "Chinese" character is 6C49. 6C49 is between 0800-FFFF, so you must use a 3-byte template: 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 completed the conversion between unicode and utf8.
If the utf-8 encoded character ch is 3 bytes. xx yy zz
AND xx and 1F to get a
AND yy and 7F to get b
AND zz and 7F to get c
(64a+b)*64+c = ch (unicode encoding)
echo.php 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. Discover the little endian and big endian problems and solve them. .
//Unicode strings stored in big endian mode cannot be recognized by ue or editplus.
$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 utf8 encoded string to unicode encoded string
* Parameter str, utf8 encoded string.
* Parameter order, the data storage 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 string
* Parameter order, unicode string. The storage order is big endian or little endian.
* Returns utf8string, the converted string.
*
*/
function unicodeToUtf8($str,$order="little")
{
$utf8string ="";
$n=strlen($str);
for ($i=0;$i turn 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 only utf8 code is accepted is because there is only formula conversion between utf8 code and unicode, other codes must be converted by looking up the code table
*. I don’t know if the regular way to find utf8 codes is completely confusing.
* Although calling utf2ucs to calculate the code value of each character is too inefficient. However, the code is clear, and it is not easy to embed the calculation process. Read.
*/
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 .
* Returns the unicode code value of the character. Once you know the code value, you can use chr to get the character.
*
* Principle: The algorithm for converting unicode to utf-8 code is.
The reverse algorithm of this process is this function, the fixed head position and the inverse sum.
*/
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 the complement of 0xC0
$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 the encoding of javascript's escape function characters after.
* I don’t know if there is any problem with the key regular search.
* Parameter: JavaScript encoded string.
* Such as: 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 alphanumeric +-_.ascii Code
$c .=$val;
}
elseif (substr($val,1,1)!="u") { //If it is a non-alphanumeric +-_. ascii code
$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;
}
?>

Current page 1/2 12Next page

The above introduces the unicode encoding converter PHP utf-8 to unicode function page 1/2, including the content of the unicode encoding converter. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles