实例
把字符串打散为数组:<?php $str = "www.gxlcms.com";print_r (explode(".",$str));?>
定义和用法
explode() 函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组。
注释:"separator" 参数不能是一个空字符串。
注释:该函数是二进制安全的。
语法explode(separator,string,limit)
// ### 切分字符串 ####
function jb51netcut($start,$end,$file){
$content=explode($start,$file);
$content=explode($end,$content[1]);
return $content[0];
}
?>
explode定义和用法
explode() 函数把字符串分割为数组。
本函数返回由字符串组成的数组,其中的每个元素都是由 separator 作为边界点分割出来的子字符串。
separator 参数不能是空字符串。如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 中单个元素的数组。
如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。
如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。此特性是 PHP 5.1.0 中新增的。
在本例中,我们将把字符串分割为数组:<?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.
)
参数 描述
separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的数目。
可能的值:
大于 0 - 返回包含最多 limit 个元素的数组
小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组
0 - 会被当做 1, 返回包含一个元素的数组
技术细节返回值:
返回字符串数组。
使用 limit 参数来返回一些数组元素:<?php
$str = 'one,two,three,four';
// 返回包含一个元素的数组
print_r(explode(',',$str,0));print "
";
// 数组元素为 2
print_r(explode(',',$str,2));print "
";
// 删除最后一个数组元素
print_r(explode(',',$str,-1));
?>