载入中
自定义HTML载入中... loading
提取utf-8里面的宽字符,转换成unicode原始编码 [转贴 2007-04-29 15:05:18]  删除... 
字体变小 字体变大

    本来用mb_substr就可以的,但是我试着写了替代方案:

<?php

    $a
="五零零+ さす ねむめほ ミムモノサセキゥィァヰン ㄆㄊㄍㄔㄧㄛㄟㄣㄠㄜㄨㄘ"
;
  

    //utf-8 string convent to unicode number array
    //提取utf-8里面的宽字符,转换成unicode原始编码

    
function utf8_to_int($str
)
    {
        
$len strlen($str
);
        
$result = array();  
//unicode number array
        
        
$code 0;            
//unicode number
        
$code_n 0;        
//utf8 byte number

        //01111111 00011111 00001111 00000111 00000011 00000001 
        
static $head_mask = array(0x7f0x1f0x0f0x070x030x01
);
        
        
        for(
$i 0$i $len$i
++)
        {
            
$byte ord$str[$i
] );

            
/* utf8 byte */ 
            
if( ($byte & ~$head_mask[5] ) == 0xfc )        
//6 byte     '1111110x 10xxxxxx ...'
            
{
                
$code_n 6
;
            }
            elseif( (
$byte & ~$head_mask[4] ) == 0xf8 )    
//5 byte    '111110xx 10xxxxxx ...'
            
{
                
$code_n 5
;
            }
            elseif( (
$byte & ~$head_mask[3] ) == 0xf0 )    
//4 byte    '11110xxx 10xxxxxx ...'
            
{
                
$code_n 4
;
            }
            elseif( (
$byte & ~$head_mask[2] ) == 0xe0 )    
//3 byte     '1110xxxx 10xxxxxx ...'
            
{
                
$code_n 3
;
            }
            elseif( (
$byte & ~$head_mask[1] ) == 0xc0 )    
//2 byte     '110xxxxx 10xxxxxx '
            
{
                
$code_n 2
;
            }
            elseif( (
$byte & ~$head_mask[0] ) == 0x00 )    
//1 byte utf8 (ansi byte) '0xxxxxxx'
            
{
                
$code_n 1
;
            }
            else                                        
//other encoding byte (no utf8)
            
{
                
//error byte
                
printf("error($i):0x%02x\n"$byte
);
                continue;
            }
            
            
//echo( "\n$i,$code_n:{". substr($str, $i, $code_n) ."}\n" );
            
            
$code $byte $head_mask[$code_n-1
];
            
//$code <<= 
            
for($j 0$j $code_n-1$j
++)
            {
                
$code <<= 6
;
                
$code |= ord$str[$i $j 1] ) & 0x3f 
;
            }
            
$i += $code_n-1
;
            
$result[] = $code
;
        }
    }

    
        
    
    
    
//$a = "中国";    
    
for($i=0$i<strlen($a); $i
++)
    {
        
printf("%2x "ord($a[$i
]) );
    }
    
utf8_to_int($a
);
?>

分类: php/shell/other
所属版块: 科技
票数:
什么是“我顶”?
点击数:    评论数:
本文章引用通告地址(TrackBack Ping URL)为:
本文章尚未被引用。
发表评论
大 名:
(不填写则显示为匿名者)
网 址:
(您的网址,可以不填)
标 题:
内 容:
请根据下图中的字符输入验证码:
(您的评论将有可能审核后才能发表)
和讯个人门户 v1.0 | 和讯部落 | 客服中心