[原创]PHP中GBK和UTF8编码处理

news/2024/6/18 21:27:19

 


一、编码范围

1. GBK (GB2312/GB18030)
/x00-/xff  GBK双字节编码范围
/x20-/x7f  ASCII
/xa1-/xff  中文
/x80-/xff  中文

2. UTF-8 (Unicode)
/u4e00-/u9fa5 (中文)
/x3130-/x318F (韩文
/xAC00-/xD7A3 (韩文)
/u0800-/u4e00 (日文)
ps: 韩文是大于[/u9fa5]的字符


正则例子:
preg_replace("/([/x80-/xff])/","",$str);
preg_replace("/([u4e00-u9fa5])/","",$str);

 

二、代码例子


//判断内容里有没有中文-GBK (PHP)
function check_is_chinese( $ s ) {
    return preg_match('/[/x80-/xff]./', $ s ) ;
}

//获取字符串长度-GBK (PHP)
function gb_strlen( $ str ) {
    $count = 0;
    for($i=0; $i<strlen($str); $ i + + ) {
        $s = substr($str, $i, 1) ;
        if (preg_match("/[/x80-/xff]/", $s)) + + $ i ;
        + + $ count ;
    }
    return $ count ;
}

//截取字符串字串-GBK (PHP)
function gb_substr($str, $ len ) {
    $count = 0;
    for($i=0; $i<strlen($str); $ i + + ) {
        if($count == $len) break ;
        if(preg_match("/[/x80-/xff]/", substr($str, $i, 1))) + + $ i ;
        ++$count;         
    
}

    return substr($str, 0, $ i ) ;
}

//统计字符串长度-UTF8 (PHP)
function utf8_strlen($str) {
    $count = 0;
    for($i = 0; $i < strlen($str); $ i + + ) {
        $value = ord ( $ str [ $ i ] ) ;
        if($value > 127) {
            $ count + + ;
            if($value >= 192 && $value <= 223) $ i + + ;
            elseif($value >= 224 && $value <= 239) $i = $i + 2;
            elseif($value >= 240 && $value <= 247) $i = $i + 3;
            else die ( 'Not a UTF-8 compatible string' ) ;
        }
        $ count + + ;
    }
    return $ count ;
}


//截取字符串-UTF8(PHP)
function utf8_substr( $ str , $ position , $ length ) {
    $start_position = strlen ( $ str ) ;
    $start_byte = 0;
    $end_position = strlen ( $ str ) ;
    $count = 0;
    for($i = 0; $i < strlen($str); $ i + + ) {
        if($count >= $position && $start_position > $ i ) {
            $start_position = $ i ;
            $start_byte = $ count ;
        }
        if(($count-$start_byte)>=$length) {
            $end_position = $ i ;
            break ;
        }     
        
$value = ord
( $ str [ $ i ] ) ;
        if($value > 127) {
            $ count + + ;
            if($value >= 192 && $value <= 223) $ i + + ;
            elseif($value >= 224 && $value <= 239) $i = $i + 2;
            elseif($value >= 240 && $value <= 247) $i = $i + 3;
            else die ( 'Not a UTF-8 compatible string' ) ;
        }
        $ count + + ;

    }
    return ( substr ( $ str , $ start_position , $ end_position - $ start_position ) ) ;
}


//字符串长度统计-UTF8 [中文3个字节,俄文、韩文占2个字节,字母占1个字节] (Ruby)
def utf8_string_length(str)
    temp = CGI::unescape(str)
    i = 0;
    j = 0;
    temp.length.times{|t|
        if temp[t] < 127
            i
+=
1
        
elseif temp[t] >= 127 and temp[t] <
224
            j
+=
1
            
if 0 == (j % 2)

                i += 2
                j
=
0
            
end

        else
            j += 1
            
if 0 == (j % 3)

                i += 2
                j
=
0
            
end

        end
    }
    return i
}


//判断是否是有韩文-UTF-8 (JavaScript)
function checkKoreaChar(str) {
    for(i=0; i<str.length; i++) {
        if(((str.charCodeAt(i) > 0x3130 && str.charCodeAt(i) < 0x318F) || (str.charCodeAt(i) >= 0xAC00 && str.charCodeAt(i) <= 0xD7A3))) {
            return true ;
        }
    }
    return false ;
}

//判断是否有中文字符-GBK (JavaScript)
function check_chinese_char(s){
    return (s.length != s.replace(/[^/x00-/xff]/g,"**").length);
}

 

三、参考文档

http://www.unicode.org/
http://examples.oreilly.com/cjkvinfo/doc/cjk.inf
http://www.ansell-uebersetzungen.com/gbuni.html
http://www.haiyan.com/steelk/navigator/ref/gbk/gbindex.htm
http://baike.baidu.com/view/40801.htm
http://www.chedong.com/tech/hello_unicode.html
 





http://www.niftyadmin.cn/n/3652575.html

相关文章

Shell编程,条件语句

文章目录条件测试操作test命令格式文件测试格式常用的测试操作符整数的比较格式常用的操作符字符串比较格式1&#xff1a;格式2&#xff1a;逻辑测试常用的操作符&#xff1a;格式补充格式补充执行顺序if语句单分支结构单分支的格式双分支结构双分支的格式多分支结构case语句结…

[原创]分表处理设计思想和实现

分表处理设计思想和实现作者&#xff1a;heiyeluren (黑夜路人)博客&#xff1a;http://blog.csdn.net/heiyeshuwu时间&#xff1a;2007-01-19 01:44:20一、概述分表是个目前算是比较炒的比较流行的概念&#xff0c;特别是在大负载的情况下&#xff0c;分表是一个良好分散数据库…

条件语句测验,if于case的运用

文章目录1.检查用户家目录中的 test.sh 文件是否存在&#xff0c;并且检查是否有执行权限2.提示用户输入100米赛跑的秒数&#xff0c;要求判断秒数大于0且小于等于10秒的进入选拔赛&#xff0c;大于10秒的都淘汰&#xff0c;如果输入其它字符则提示重新输入&#xff1b;进入选拔…

[转]一个功能强大的免费路由防火墙软件-pfSense

来源&#xff1a; http://blog.chinaunix.net/u/15930/showart.php?id123755这些天一直在网上寻找怎样自自己作一个LINUX下的路由防火墙&#xff0c;并能存在于一个U盘中。我在此版发过这样的一个话题&#xff0c;可是回答的人很少且都没有提供制作方法。今天重新翻看&#xf…

补充:echo的用法

echo -n 表示不换行输出 echo -e 输出转义字符&#xff0c;将转义后的内容输出到屏幕上 常用的转义字符如下&#xff1a; \b 转义后相当于按退格键&#xff08;backspace&#xff09;&#xff0c;但前提是"\b"后面存在字符&#xff1b;"\b"表示删除前一个…

for,while,untill循环,以及break跳出和continue中止循环

文章目录for循环格式变量i的三种不同格式for i in { } 用法for i in $(seq 1 10)for ((i1;i<10;i))实例while语句格式格式举例实例until语句格式双循环break跳出循环break跳出两层循环continue中止某次循环中的命令&#xff0c;但不会完全中止整个命令IFS字段分隔符实例for循…

[转]开发大型高负载类网站应用的几个要点

开发大型高负载类网站应用的几个要点作者&#xff1a;nightsailer 来源&#xff1a;http://www.phpchina.com/bbs/thread-15484-1-1.html看了一些人的所谓大型项目的方法,我感觉都是没有说到点子上&#xff0c;有点难受。我也说说自己的看法.我个人认为,很难衡量所谓项目是否大…

for多循环,组成图形

文章目录双层循环的应用乘法表直角三角形一二三四等腰三角形正等腰三角形倒立的等腰三角形菱形四边形长方形平行四边形梯形等腰梯形双层循环的应用 乘法表 直角三角形 一 二 三 四 等腰三角形 正等腰三角形 倒立的等腰三角形 菱形 四边形 长方形 平行四边形 梯形 等腰梯形