正在显示
1 个修改的文件
包含
4 行增加
和
9 行删除
| @@ -1597,29 +1597,24 @@ if (!function_exists('httpGetSsl')) { | @@ -1597,29 +1597,24 @@ if (!function_exists('httpGetSsl')) { | ||
| 1597 | * @time :2025/9/22 14:46 | 1597 | * @time :2025/9/22 14:46 |
| 1598 | */ | 1598 | */ |
| 1599 | function truncate_text($text, $limit = 300) { | 1599 | function truncate_text($text, $limit = 300) { |
| 1600 | - // 长度小于限制直接返回 | ||
| 1601 | if (mb_strlen($text, 'UTF-8') <= $limit) { | 1600 | if (mb_strlen($text, 'UTF-8') <= $limit) { |
| 1602 | return $text; | 1601 | return $text; |
| 1603 | } | 1602 | } |
| 1604 | - // 先取前 $limit 个字符 | ||
| 1605 | $truncated = mb_substr($text, 0, $limit, 'UTF-8'); | 1603 | $truncated = mb_substr($text, 0, $limit, 'UTF-8'); |
| 1606 | // 优先找空格 | 1604 | // 优先找空格 |
| 1607 | $lastSpace = mb_strrpos($truncated, ' ', 0, 'UTF-8'); | 1605 | $lastSpace = mb_strrpos($truncated, ' ', 0, 'UTF-8'); |
| 1608 | if ($lastSpace === false) { | 1606 | if ($lastSpace === false) { |
| 1609 | - // 如果没有空格,尝试找中横线或下划线(如英文连接符) | ||
| 1610 | - $lastDash = max( | ||
| 1611 | - (mb_strrpos($truncated, '-', 0, 'UTF-8') ?: 0), | ||
| 1612 | - (mb_strrpos($truncated, '_', 0, 'UTF-8') ?: 0)) | ||
| 1613 | - ; | ||
| 1614 | - if ($lastDash > 0) { | 1607 | + // 没有空格则找 '-' |
| 1608 | + $lastDash = mb_strrpos($truncated, '-', 0, 'UTF-8'); | ||
| 1609 | + if ($lastDash !== false && $lastDash > 0) { | ||
| 1615 | $lastSpace = $lastDash; | 1610 | $lastSpace = $lastDash; |
| 1616 | } | 1611 | } |
| 1617 | } | 1612 | } |
| 1618 | - // 若找到合适位置则截断 | ||
| 1619 | if ($lastSpace !== false && $lastSpace > 0) { | 1613 | if ($lastSpace !== false && $lastSpace > 0) { |
| 1620 | $truncated = mb_substr($truncated, 0, $lastSpace, 'UTF-8'); | 1614 | $truncated = mb_substr($truncated, 0, $lastSpace, 'UTF-8'); |
| 1621 | } | 1615 | } |
| 1622 | return $truncated; | 1616 | return $truncated; |
| 1623 | } | 1617 | } |
| 1624 | 1618 | ||
| 1619 | + | ||
| 1625 | } | 1620 | } |
-
请 注册 或 登录 后发表评论