用于分割字符串。
相关函数如下:
本函数为 implode() 的反函数,使用一个字符串分割另一个字符串,返回一个数组。
语法:
array explode( string separator, string string [, int limit] )
| 参数 | 说明 |
|---|---|
| separator | 分割标志 |
| string | 需要分割的字符串 |
| limit | 可选,表示返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分,支持负数。 |
例子:
<?php
$str = 'one|two|three|four';
print_r(explode('|', $str));
print_r(explode('|', $str, 2));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode('|', $str, -1));
?>
输出结果如下:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)