作者 李宇航

合并分支 'lyh-server' 到 'master'

变更数据



查看合并请求 !3042
... ... @@ -1597,29 +1597,24 @@ if (!function_exists('httpGetSsl')) {
* @time :2025/9/22 14:46
*/
function truncate_text($text, $limit = 300) {
// 长度小于限制直接返回
if (mb_strlen($text, 'UTF-8') <= $limit) {
return $text;
}
// 先取前 $limit 个字符
$truncated = mb_substr($text, 0, $limit, 'UTF-8');
// 优先找空格
$lastSpace = mb_strrpos($truncated, ' ', 0, 'UTF-8');
if ($lastSpace === false) {
// 如果没有空格,尝试找中横线或下划线(如英文连接符)
$lastDash = max(
(mb_strrpos($truncated, '-', 0, 'UTF-8') ?: 0),
(mb_strrpos($truncated, '_', 0, 'UTF-8') ?: 0))
;
if ($lastDash > 0) {
// 没有空格则找 '-'
$lastDash = mb_strrpos($truncated, '-', 0, 'UTF-8');
if ($lastDash !== false && $lastDash > 0) {
$lastSpace = $lastDash;
}
}
// 若找到合适位置则截断
if ($lastSpace !== false && $lastSpace > 0) {
$truncated = mb_substr($truncated, 0, $lastSpace, 'UTF-8');
}
return $truncated;
}
}
... ...