迅睿CMS框架是一款PHP8高性能·简单易用的CMS开源开发框架,基于MIT开源许可协议发布,免费且不限制商业使用,是免费开源的产品,以万端互联为设计理念,支持的微信公众号、小程序、APP客户端、移动端网站、PC网站等多终端式管理系统。
联系官方销售客服
1835022288
028-61286886
除了dr_strcut方法可以截取,适用于中文,有其它方法是自动识别中英文来截取完成的长度吗?
if (!function_exists('dr_en_strcut')) { function dr_en_strcut($str, $limit = '100', $dot = '...') { if (!$str) { return ''; } $start = 0; if ($limit && strpos((string)$limit, ',')) { list($start, $length) = explode(',', $limit); $length = (int)$length; } else { $length = (int)$limit; } if (strlen($str) <= $length || !$length) { return $str; } $res = substr($str, $start, $length); $strlen = strlen($str); if ($start >= 0) { $next_start = $start + $length; $next_len = $next_start + 6 <= $strlen ? 6 : $strlen - $next_start; $next_segm = substr($str, $next_start, $next_len); $prev_start = $start - 6 > 0 ? $start - 6 : 0; $prev_segm = substr($str, $prev_start, $start - $prev_start); } else { $next_start = $strlen + $start + $length; $next_len = $next_start + 6 <= $strlen ? 6 : $strlen - $next_start; $next_segm = substr($str, $next_start, $next_len); $start = $strlen + $start; $prev_start = $start - 6 > 0 ? $start - 6 : 0; $prev_segm = substr($str, $prev_start, $start - $prev_start); } if (preg_match('@^([x80-xBF]{0,5})[xC0-xFD]?@', $next_segm, $bytes)) { if (!empty($bytes[1])) { $bytes = $bytes[1]; $res .= $bytes; } } $ord0 = ord($res[0]); if (128 <= $ord0 && 191 >= $ord0) { if (preg_match('@[xC0-xFD][x80-xBF]{0,5}$@', $prev_segm, $bytes)) { if (!empty($bytes[0])) { $bytes = $bytes[0]; $res = $bytes . $res; } } } if (strlen($res) < $strlen) { $res = $res . $dot; } return $res; } }