@ 2013-03-31T16:42:53Z <?php
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if($suffix)
$suffixStr = "…";
else
$suffixStr = "";
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset).$suffixStr;
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset).$suffixStr;
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
return $slice.$suffixStr;
}
function u8_title_substr($str, $width = 0, $end = '...', $x3 = 0) {
global $CFG; // 全局变量保存 x3 的值
if ($width <= 0 || $width >= strlen($str)) {
return $str;
}
$arr = str_split($str);
$len = count($arr);
$w = 0;
$width *= 10;
// 不同字节编码字符宽度系数
$x1 = 11; // ASCII
$x2 = 16;
$x3 = $x3===0 ? ( $CFG['cf3'] > 0 ? $CFG['cf3']*10 : $x3 = 21 ) : $x3*10;
$x4 = $x3;
// http://zh.wikipedia.org/zh-cn/UTF8
for ($i = 0; $i < $len; $i++) {
if ($w >= $width) {
$e = $end;
break;
}
$c = ord($arr[$i]);
if ($c <= 127) {
$w += $x1;
}
elseif ($c >= 192 && $c <= 223) { // 2字节头
$w += $x2;
$i += 1;
}
elseif ($c >= 224 && $c <= 239) { // 3字节头
$w += $x3;
$i += 2;
}
elseif ($c >= 240 && $c <= 247) { // 4字节头
$w += $x4;
$i += 3;
}
}
return implode('', array_slice($arr, 0, $i) ). $e;
}
$a = '测试这是一个测试';
//$a = 'xxxxbbbbbnnn';
$b = u8_title_substr($a,0,5,10);
var_dump($b);
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Deprecated: Optional parameter $start declared before required parameter $length is implicitly treated as a required parameter in /in/BsZbI on line 3
string(24) "测试这是一个测试"
Output for 8.0.0 - 8.0.30 Deprecated: Required parameter $length follows optional parameter $start in /in/BsZbI on line 3
string(24) "测试这是一个测试"
Output for 4.3.0 - 4.3.11 , 4.4.0 - 4.4.9 , 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 string(24) "测试这是一个测试"
preferences:dark mode live preview
293.8 ms | 405 KiB | 460 Q