函式名:mb_split()
適用版本:PHP 4 >= 4.2.0, PHP 5, PHP 7
函式說明:mb_split() 函式使用多位元組字符集進行字串分割,並返回一個由分割後的子字串組成的陣列。
語法:mb_split(string $pattern, string $string [, int $limit = -1])
引數:
- $pattern:指定的分隔符,可以是一個字串或正規表示式。
- $string:要進行分割的字串。
- $limit(可選):指定返回陣列的最大元素個數。預設值為-1,表示不限制返回的陣列元素個數。
返回值:返回一個由分割後的子字串組成的陣列,如果分割失敗則返回 FALSE。
示例:
// 使用空格分割字串
$str = "Hello World";
$result = mb_split(" ", $str);
print_r($result);
// Output: Array ( [0] => Hello [1] => World )
// 使用正規表示式分割字串
$str = "Hello,World";
$result = mb_split("[,]", $str);
print_r($result);
// Output: Array ( [0] => Hello [1] => World )
// 限制返回的陣列元素個數
$str = "Hello World";
$result = mb_split(" ", $str, 1);
print_r($result);
// Output: Array ( [0] => Hello )
注意事項:
- mb_split() 函式使用的是多位元組字符集進行分割,需要確保 PHP 環境已啟用 mbstring 擴充套件。
- 如果分割失敗,mb_split() 函式將返回 FALSE。
- 如果不指定 $limit 引數,將返回所有分割後的子字串。