ca88编程php达成utf-8转unicode函数代码实例

作者:ca88编程

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);
}

<?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];
        $c2 = $name[$i 1];
        if (ord($c) > 0)
        {   //八个字节的文字
            $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;
}

复制代码 代码如下:

public function utf8_unicode($str) {
    $unicode = array();
    $values = array();
    $lookingFor = 1;

ca88编程,复制代码 代码如下:

复制代码 代码如下:

  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;
    }

对于这种主题材料,我们照旧平昔上代码来的快,大伙拿去一贯用啊,不要谢小编。

本文由ca88发布,转载请注明来源

关键词: ca88网址 Ca88电脑版