在PHP中,explode()
函數用于將一個字符串分割為數組
explode()
函數接受兩個參數:分隔符和要分割的字符串。explode(string $delimiter, string $string, int $limit = PHP_INT_MAX): array
,
將字符串分割為數組。$string = "apple,banana,orange";
$array = explode(',', $string);
print_r($array); // 輸出:Array ( [0] => apple [1] => banana [2] => orange )
PHP_INT_MAX
,表示沒有限制。$string = "apple,banana,orange,grape";
$array = explode(',', $string, 2); // 只返回前兩個元素
print_r($array); // 輸出:Array ( [0] => apple [1] => banana )
explode()
函數。$string = "apple;banana|orange,grape";
$delimiters = [';', '|', ','];
$array = explode($delimiters, $string);
print_r($array); // 輸出:Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
通過這些方法,您可以根據需要控制explode()
函數在PHP中的行為。