PHP explode() 函數
實例
把字符串打散為數組:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
運行實例»
定義和用法
explode() 函數把字符串打散為數組。
註釋: "separator"參數不能是一個空字符串。
註釋:該函數是二進制安全的。
語法
explode( separator,string,limit )
参数 | 描述 |
---|---|
separator | 必需。规定在哪里分割字符串。 |
string | 必需。要分割的字符串。 |
limit | 可选。规定所返回的数组元素的数目。 可能的值:
|
技術細節
返回值: | 返回字符串數組。 |
---|---|
PHP 版本: | 4+ |
更新日誌: | 在PHP 4.0.1中,新增了limit參數。 在PHP 5.1.0中,新增了對負數limits的支持。 |
更多實例
實例1
使用limit 參數來返回一些數組元素:
<?php
$str = 'one,two,three,four';
// zero limit
print_r(explode(',',$str,0));
// positive limit
print_r(explode(',',$str,2));
// negative limit
print_r(explode(',',$str,-1));
?>
$str = 'one,two,three,four';
// zero limit
print_r(explode(',',$str,0));
// positive limit
print_r(explode(',',$str,2));
// negative limit
print_r(explode(',',$str,-1));
?>
運行實例»
PHP String參考手冊