Home  >  Article  >  Backend Development  >  PHP 字符串编码截取函数(兼容utf-8和gb2312)_php实例

PHP 字符串编码截取函数(兼容utf-8和gb2312)_php实例

WBOY
WBOYOriginal
2016-06-07 17:26:25668browse
复制代码 代码如下:

//截取字符串长度。支持utf-8和gb2312编码。若为gb2312,先将其转为utf-8,在utf-8的基础上截取然后再转换回来
function cut_string($str,$from=1,$length=10,$code='utf-8',$rear='...'){
    if($code!='utf-8'){//总是将字符串转为utf-8编码
        $str=iconv($code,'utf-8',$str);
    }
    $str_len=mb_strlen($str,'utf-8');//字符串的长度
    if($from>$str_len){//如果截取开始位置大于字符串长度,截取后面$length个
        $from=$str_len-$length+1;
        $from=($from    }
    //兼容ucs-4编码
    $i=0;//字节计数
    $from_i=0;//开始截取的字节位置
    $from_len=0;//开始截取的字符位置
    $tag=true;//标志$from_len是否已经被赋值
    for($temp_len=0;($temp_len-$from_len        $byte_code=ord(substr($str,$i,1));//一个字节的编码
        if($temp_len+1==$from){//记录开始截取的开始字节位置
            $from_i=$i;$from_len=$temp_len;$tag=false;
        }
        if($byte_code>=0&&$byte_code            $i++;
        }
        if($byte_code>191&&$byte_code            $i+=2;
        }
        if($byte_code>223&&$byte_code            $i+=3;
        }
        if($byte_code>239&&$byte_code            $i+=4;
        }
        if($byte_code>248&&$byte_code            $i+=5;
        }
        if($byte_code>252&&$byte_code            $i+=6;
        }
    }
    return iconv('utf-8',$code,substr($str,$from_i,$i-$from_i).$rear);
}
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