PHP中的split()函數(shù)已經在PHP 5.3.0版本中被廢棄,從PHP 7.0.0版本起已經被移除。建議使用preg_split()函數(shù)來代替。
preg_split()函數(shù)用于將字符串按照正則表達式模式進行拆分。它的用法如下:
preg_split(pattern, subject, limit, flags)
參數(shù)說明:
返回值:
示例:
$str = "Hello World! This is a PHP string.";
$pattern = "/[\s,]+/"; // 按照空格和逗號進行拆分
$result = preg_split($pattern, $str);
print_r($result);
輸出:
Array
(
[0] => Hello
[1] => World!
[2] => This
[3] => is
[4] => a
[5] => PHP
[6] => string.
)
上述示例中,字符串$str被按照空格和逗號進行拆分,拆分后的結果保存在數(shù)組$result中。