函式名:mb_strimwidth()
適用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7
用法:mb_strimwidth(string $str, int $start, int $width, string $trimmarker = "", string $encoding = null): string
說明:mb_strimwidth() 函式根據指定的寬度截斷字串,並在末尾新增一個可選的修飾符。該函式是多位元組安全的,可以正確處理包含多位元組字元的字串。
引數:
- $str:要截斷的字串。
- $start:截斷開始的位置,可以為負數。
- $width:截斷後的字串長度。
- $trimmarker(可選):新增到截斷字串末尾的修飾符,預設為空字串。
- $encoding(可選):指定字元編碼,預設為內部字元編碼。
返回值:返回截斷後的字串。
示例:
$text = "這是一個示例文字,用於演示mb_strimwidth()函式的用法";
$trimmedText = mb_strimwidth($text, 0, 20, "...");
echo $trimmedText;
// 輸出:這是一個示例文字...
在上面的示例中,我們使用 mb_strimwidth() 函式將文字截斷為長度為 20 的字串,如果截斷,則在末尾新增了省略號 "..."。