Home >Backend Development >PHP Tutorial >PHP implements utf-8 to unicode function sharing, utf-8unicode_PHP tutorial

PHP implements utf-8 to unicode function sharing, utf-8unicode_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:15893browse

PHP implements utf-8 to unicode function sharing, utf-8unicode

The code is very simple, but the function is very practical. I recommend it to everyone.

Here’s the code first:

Copy code The code is as follows:

public function utf8_unicode($str) {
$unicode = array();
$values ​​= array();
$lookingFor = 1;
for ($i = 0; $i < strlen( $str ); $i++ ) {
          $thisValue = ord( $str[ $i ] );
If ( $thisValue < ord('A') ) {
                         // exclude 0-9
If ($thisValue >= ord('0') && $thisValue <= ord('9')) {
                                 // number
                        $unicode[] = chr($thisValue);
            }
             else {
                          $unicode[] = '%'.dechex($thisValue);
            }
         } else {
If ( $thisValue < 128) {
                    $unicode[] = $str[ $i ];
              } else {
If ( count( $values ​​) == 0 ) {
$lookingFor = ( $thisValue < 224 ) ? 2 : 3;
                }
$values[] = $thisValue;
If ( count( $values ​​) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
                             $number = dechex($number);
$unicode[] = (strlen($number)==3)?"u0".$number:"u".$number;
                          $values ​​= array();
$lookingFor = 1;
                     } // if
                } // if
}
} // for
Return implode("",$unicode);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/938848.htmlTechArticlePHP implements utf-8 to unicode function sharing. The utf-8unicode code is very simple, but the function is very practical. It is recommended to Everyone. Submit the code first: Copy the code. The code is as follows: public function utf8_uni...
Statement:
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