explode

(PHP 3, PHP 4, PHP 5)

explode -- 使用一个字符串分割另一个字符串

描述

array explode ( string separator, string string [, int limit] )

此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。

如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。

如果 limit 参数是负数,则返回除了最后的 limit 个元素外的所有元素。此特性是 PHP 5.1.0 中新增的。

由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。

注: 参数 limit 是在 PHP 4.0.1 中加入的。

例子 1. explode() 示例

<?php
// 示例 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo
$pieces[0]; // piece1
echo $pieces[1]; // piece2

// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo
$user; // foo
echo $pass; // *

?>

例子 2. limit 参数示例

<?php
$str
= 'one|two|three|four';

// 正数的 limit
print_r(explode('|', $str, 2));

// 负数的 limit
print_r(explode('|', $str, -1));
?>

以上示例将输出:

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

注: 本函数可安全用于二进制对象。

参见 preg_split()spliti()split()implode()


add a note add a note User Contributed Notes
Elad Elrom
21-Oct-2006 02:50
// simple function to remove words if more than max allowed words or add a charcter once less than min
// Example: LimitText("The red dog ran out of thefence",15,20,"<br>");

function LimitText($Text,$Min,$Max,$MinAddChar) {
   if (strlen($Text) < $Min) {
       $Limit = $Min-strlen($Text);
       $Text .= $MinAddChar;
   }
   elseif (strlen($Text) >= $Max) {
       $words = explode(" ", $Text);
       $check=1;
       while (strlen($Text) >= $Max) {
           $c=count($words)-$check;           
           $Text=substr($Text,0,(strlen($words[$c])+1)*(-1));
           $check++;
       }
   }
  
   return $Text;
}
webmaster at saund-web dot com
14-Mar-2006 02:20
If you want to split a price (float) into pounds and pence.

or dollors and cents etc etc.       

$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo "&pound $pound . $pence\n";
jeppe at bundsgaard dot net
18-Sep-2005 02:08
In version 5.05 (I have heard it is the same in 5.1) I suddenly got the error "only variables can be passed by reference" in connection to "array_pop(explode())" - it affects a lot of other combinations too (array_shift, array_keys, list etc).

The solution is simple (and it is possible to do a global search and replace):

replace array_pop(explode()) with array_pop($dummyarr=explode()).
x403 at yandex dot ru
24-Jul-2005 08:09
For parsing URL better use Preg_split with flag PREG_SPLIT_NO_EMPTY because Explode or Split functions set empty array elements. For example:

$parts = preg_split("/\//i", $_SERVER["REQUEST_URI"], -1, PREG_SPLIT_NO_EMPTY);

result: Array ( [0] => mix [1] => exhibit )

Explode("/",$_SERVER["REQUEST_URI"] )

result: Array ( [0] => [1] => [2] => mix [3] => exhibit )
emilyd at boreal (.) org
24-Feb-2005 05:20
Also, it seems any array element (at least using explode) is limited to 255 characters.
djogo_curl at yahoo
01-Dec-2004 08:50
Being a beginner in php but not so in Perl, I was used to split() instead of explode(). But as split() works with regexps it turned out to be much slower than explode(), when working with single characters.
ian at illumen dot co dot uk
24-Aug-2004 04:30
If you split an empty string, you get back a one-element array with 0 as the key and an empty string for the value.

<?php

$str
= '';

$foo = explode( ":", $str );
print_r( $foo );

$foo = split( ":", $str );
print_r( $foo );

$foo = preg_split( "/:/", $str );
print_r( $foo );

?>

In all three cases prints

Array
(
  [0] =>
)

This of course means that you must explicitly check to see if the value of the first element is blank, or must check to see if the string being split is blank.
coroa at cosmo-genics dot com
17-Nov-2003 12:01
To split a string containing multiple seperators between elements rather use preg_split than explode:

preg_split ("/\s+/", "Here  are    to    many  spaces in  between");

which gives you
array ("Here", "are", "to", "many", "spaces", "in", "between");
ralfoide at yahoo dat com
14-Jul-2003 06:02
Using 1 or less for the "limit" may not yield the result you expect. I'm using PHP 4.1.2

I had surprising results when using explode() or split() with a "limit" of 0 or 1: in this case the returned array contains one entry with the full "remaining" string.

Ex:
$a = explode("/", "a/b/c", 1); var_dump($a);
=> array(1) { [0]=>  string(5) "a/b/c" }

$a = explode("/", "a/b/c", 2); var_dump($a);
=> array(2) { [0]=>  string(1) "a" [1]=>  string(3) "b/c" }

Using limit=0 behaves as in limit=1 above.
It seems the implementation uses limit as the number of elements to return, including the "remaining string", whereas the doc seems to describe it as indicating the number of separators to process (i.e. the resulting array should contain limit+1 entries).