/**
- 截取UTF8编码字符串从首字节开始指定宽度(非长度), 适用于字符串长度有限的如新闻标题的等宽度截取
- 中英文混排情况较理想. 全中文与全英文截取后对比显示宽度差异最大,且截取宽度远大越明显.
- @param string $str UTF-8 encoding
- @param int[option] $width 截取宽度
- @param string[option] $end 被截取后追加的尾字符
- @param float[option] $x3
- 3字节(中文)字符相当于希腊字母宽度的系数coefficient(小数)
- 中文通常固定用宋体,根据ascii字符字体宽度设定,不同浏览器可能会有不同显示效果
*/
function u8_title_substr($str, $width = 0, $end = '...', $x3 = 0) {
global
width <= 0 ||
str)) { return
e = '';
str);
arr);
width *= 10; // 不同字节编码字符宽度系数
x2 = 16; $x3 =
CFG['cf3']) ? $CFG['cf3']*10 : $x3 = 21 ) :
x3 = $x3===0 ? ( $CFG['cf3'] > 0 ? $CFG['cf3']*10 : $x3 = 21 ) :
x4 = $x3;
// http://zh.wikipedia.org/zh-cn/UTF8 for ($i = 0; $i < $len;w >= $width) { $e = $end; break; }
arr[
c <= 127) { $w +=
c >= 192 && $c <= 223) { // 2字节头 $w += $x2;
c >= 224 && $c <= 239) { // 3字节头 $w += $x3;
c >= 240 && $c <= 247) { // 4字节头 $w += $x4; $i += 3; } }
return @implode('', array_slice($arr, 0, $i) ). $e; }
/******************************************************************
- PHP截取UTF-8字符串,解决半字符问题。
- 英文、数字(半角)为1字节(8位),中文(全角)为3字节
- @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串
- @param $str 源字符串
str,
i=0;
len;$i++) {
str,0,1); if(ord($temp_str) > 127) {
i<$len) {
str,0,3);
str,3); } } else {
str,0,1);
str,1); } } return join($new_str); }