当前位置:编程学习 > php >>

php explode 实例与教程

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 单个元素的数组。

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

<?php
$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.
)
实例二

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

for($index=0;$index<count($hello);$index++){
echo $hello[$index];echo "</br>";
}

实例三

<?php
$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() 函数实例教程

补充:Php教程,Php常用代码 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,