{$vo.title|msubstr=0,5,'utf-8',false}
0:起始位置 (從第 0 個字元開始)5:截取長度 (5 個字元)'utf-8':字元編碼false:不顯示省略號 (true 則顯示 ...)msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
| 參數 | 說明 | 預設值 |
|---|---|---|
| $str | 要截取的字串 | 必填 |
| $start | 起始位置 | 0 |
| $length | 截取長度 | 必填 |
| $charset | 字元編碼 | utf-8 |
| $suffix | 是否加省略號 | true |
若提示函數不存在,需載入擴展函數庫:
// 在 Action/Controller 中載入
Load('extend');
經測試官方版本無法正常顯示省略號,以下為修正版本 (修改 Common/extend.php):
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
if (function_exists("mb_substr")) {
$result = mb_substr($str, $start, $length, $charset);
return $suffix ? $result . "..." : $result;
} elseif (function_exists('iconv_substr')) {
$result = iconv_substr($str, $start, $length, $charset);
return $suffix ? $result . "..." : $result;
}
// 純 PHP 實現 (無 mbstring/iconv 環境)
$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 $suffix ? $slice . "…" : $slice;
}
mbstring 擴展,效能最佳text-overflow: ellipsis 替代