Latest web development tutorials

PHP explode () function

PHP String Reference PHP String Reference

Examples

String broken array:

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

Running instance »

Definition and Usage

explode () function to break up the string array.

Note: "separator" parameter can not be an empty string.

Note: This function is binary safe.


grammar

explode( separator,string,limit )

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

可能的值:

  • 大于 0 - 返回包含最多 limit 个元素的数组
  • 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组
  • 0 - 返回包含一个元素的数组

technical details

return value: Returns an array of strings.
PHP version: 4+
Update log: In PHP 4.0.1, add the parameter limit. In PHP 5.1.0, the new support for the negative limits.


More examples

Example 1

Use limit parameter to return some of the array elements:

<?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));
?>

Running instance »


PHP String Reference PHP String Reference