Home  >  Article  >  php教程  >  php explode 实例与教程

php explode 实例与教程

WBOY
WBOYOriginal
2016-06-08 17:29:031161browse
<script>ec(2);</script>

php explode 实例与教程
array explode ( string separator, string string [, int limit] )

separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的最大数目。


每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。

如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包

含 string 的剩余部分。

如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包

含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。

在本例中,我们将把字符串分割为数组:

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
实例二

//利用 explode 函数分割字符串到数组
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$hello = explode(',',$source);

for($index=0;$index echo $hello[$index];echo "";
}

实例三

$foo = 'uno dos  tres'; // two spaces between "dos" and "tres"
print_r(explode(' ', $foo));
?>

Array
(
    [0] => uno
    [1] => dos
    [2] =>
    [3] => tres
)
请记住,explode()可以返回,如果分隔立即重复两次(或更多空元素),如以下示

例所示:

PHP implode() 函数实例教程

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php do while 语句Next article:php while loop next