msubstr 字符截取函數完整用法與常見坑位避坑指南msubstr 是 ThinkPHP 內置的多字節安全字符截取函數,解決 substr 截取中文亂碼問題。本文詳解語法、模板調用、核心源碼與 Linux 部署常見大小寫陷阱。
msubstr($str, $start = 0, $length, $charset = 'utf-8', $suffix = true)
| 參數 | 說明 | 默認值 |
|---|---|---|
$str | 待截取字符串 | 必填 |
$start | 開始位置(0 索引) | 0 |
$length | 截取長度 | 必填 |
$charset | 字符編碼 | 'utf-8' |
$suffix | 是否添加省略號 | true |
{-- 從第 5 個字符開始,截取 5 個字符,UTF-8,不顯示省略號 --}
{$vo.title|msubstr=5,5,'utf-8',false}
{-- 從開頭截取 20 個字符,顯示省略號 --}
{$vo.content|msubstr=0,20}
{-- GBK 編碼環境 --}
{$vo.name|msubstr=0,10,'gbk',true}
function msubstr($str, $start = 0, $length, $charset = 'utf-8', $suffix = true) {
// 1. 優先使用 mbstring 擴展
if (function_exists('mb_substr')) {
$slice = mb_substr($str, $start, $length, $charset);
return $suffix ? $slice . '...' : $slice;
}
// 2. 其次使用 iconv
if (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
return $suffix ? $slice . '...' : $slice;
}
// 3. 純 PHP 正則兜底(性能較差)
$re = [
'utf-8' => '/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/',
'gb2312' => '/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/',
'gbk' => '/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/',
'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;
}
Call to undefined function msubstr()Common/common.php(C 大寫目錄,小寫文件)# 錯誤:目錄大寫,文件大寫 Common/Common.php # 錯誤:目錄小寫 common/common.php # 正確:目錄大寫,文件小寫 Common/common.php
Application/Common/common.phpcommon.php 中定義 msubstr 函數(或引入框架核心函數庫)php think clear 或刪除 Runtime/Cache/ 目錄php -r "var_dump(function_exists('msubstr'));"<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller {
public function index() {
$title = '這是一個很長的標題需要截取顯示';
$short = msubstr($title, 0, 15, 'utf-8', true);
$this->assign('short_title', $short);
return $this->fetch();
}
}
str_limit// TP 5.1+ 內置 str_limit($str, 20, '...'); // 等同 msubstr($str, 0, 20, 'utf-8', true) // 更強大的字符串操作類 use think\Str; Str::limit($str, 20); // 截取 Str::contains($str, '關鍵詞'); // 包含檢查 Str::startsWith($str, '前綴'); // 前綴匹配 Str::endsWith($str, '後綴'); // 後綴匹配
mb_substr 直接使用// 現代 PHP 項目推薦直接使用 mbstring
mb_substr($str, 0, 20, 'UTF-8'); // 無需封裝
// 組合函數
function safe_substr($str, $len, $suffix = '...') {
return mb_strlen($str, 'UTF-8') > $len
? mb_substr($str, 0, $len, 'UTF-8') . $suffix
: $str;
}
| 方案 | 性能 | 依賴 | 推薦場景 |
|---|---|---|---|
mb_substr | ⭐⭐⭐⭐⭐ | mbstring 擴展 | 現代 PHP 7.2+ 項目首選 |
iconv_substr | ⭐⭐⭐⭐ | iconv 擴展 | 無 mbstring 環境 |
msubstr (正則兜底) | ⭐⭐ | 無 | 極舊環境兼容 |
Str::limit (TP 5.1+) | ⭐⭐⭐⭐ | ThinkPHP 核心 | TP 框架項目統一規範 |
php-mbstring / php-iconvCommon/common.php(目錄大寫,文件小寫)起始,長度,編碼,省略號掌握 msubstr 原理與部署細節,能徹底解決 ThinkPHP 專案中中文截取亂碼與函數未定義的經典問題。