獲取字元的一部分

mb_strcut

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_strcut獲取字元的一部分

說明

mb_strcut(
    string $str,
    int $start,
    int $length = NULL,
    string $encoding = mb_internal_encoding()
): string

mb_strcut()mb_substr() 類似,都是從字串中提取子字串,但是按位元組數來執行,而不是字元個數。 如果截斷位置位於多位元組字元兩個位元組的中間,將於該字元的第一個位元組開始執行。 這也是和 substr() 函式的不同之處,後者簡單地將字串在位元組之間截斷,這將導致一個畸形的位元組序列。

參數

str

要截斷的 string

start

如果 start 不是負數,返回的字串會從 str 的第 start 位元組位置開始,從 0 開始計數。舉個例子,字串 'abcdef',位元組位置 0 的字元是 'a',位元組位置 2 的字元是 'c',以此類推。

如果 start 是負數,返回的字串是從 str 末尾處第 start 個位元組開始的。

length

位元組長度。If omitted or NULL is passed, extract all bytes to the end of the string.

encoding

encoding 參數為字元編碼。如果省略或是 null,則使用內部字元編碼。

返回值

mb_strcut() 根據 startlength 參數返回 str 的一部分。

更新日誌

版本 說明
5.4.8 Passing NULL as length extracts all bytes to the end of the string. Prior to this version NULL was treated the same as 0.

參見

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *