复制代码 代码如下:
/*
* 功能: 作用跟substr一样,除了它不会造成乱码
* 参数:
* 返回:
*/
function utf8_substr( $str , $start , $length=null ){
// 先正常截取一遍.
$res = substr( $str , $start , $length );
$strlen = strlen( $str );
/* 接着判断头尾各6字节是否完整(不残缺) */
// 如果参数start是正数
if ( $start >= 0 ){
// 往前再截取大约6字节
$next_start = $start + $length; // 初始位置
$next_len = $next_start + 6 $next_segm = substr( $str , $next_start , $next_len );
// 如果第1字节就不是 完整字符的首字节, 再往后截取大约6字节
$prev_start = $start - 6 > 0 ? $start - 6 : 0;
$prev_segm = substr( $str , $prev_start , $start - $prev_start );
}
// start是负数
else{
// 往前再截取大约6字节
$next_start = $strlen + $start + $length; // 初始位置
$next_len = $next_start + 6 $next_segm = substr( $str , $next_start , $next_len );
// 如果第1字节就不是 完整字符的首字节, 再往后截取大约6字节.
$start = $strlen + $start;
$prev_start = $start - 6 > 0 ? $start - 6 : 0;
$prev_segm = substr( $str , $prev_start , $start - $prev_start );
}
// 判断前6字节是否符合utf8规则
if ( preg_match( '@^([\x80-\xBF]{0,5})[\xC0-\xFD]?@' , $next_segm , $bytes ) ){
if ( !empty( $bytes[1] ) ){
$bytes = $bytes[1];
$res .= $bytes;
}
}
// 判断后6字节是否符合utf8规则
$ord0 = ord( $res[0] );
if ( 128 = $ord0 ){
// 往后截取 , 并加在res的前面.
if ( preg_match( '@[\xC0-\xFD][\x80-\xBF]{0,5}$@' , $prev_segm , $bytes ) ){
if ( !empty( $bytes[0] ) ){
$bytes = $bytes[0];
$res = $bytes . $res;
}
}
}
return $res;
}
测试数据::
复制代码 代码如下:
$str = 'dfjdjf测13f试65&2数据fdj(1就mfe&……就';
var_dump( utf8_substr( $str , 22 , 12 ) ); echo '
';
var_dump( utf8_substr( $str , 22 , -6 ) ); echo '
';
var_dump( utf8_substr( $str , 9 , 12 ) ); echo '
';
var_dump( utf8_substr( $str , 19 , 12 ) ); echo '
';
var_dump( utf8_substr( $str , 28 , -6 ) ); echo '
';
显示结果::(截取无乱码, 欢迎大家测试, 提交bug)
string(12) "据fdj"
string(26) "据fdj(1就mfe&…"
string(13) "13f试65&2数"
string(12) "数据fd"
string(20) "dj(1就mfe&…"
Statement of this Website
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