PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

fprintf> <echo
Last updated: Sun, 25 Nov 2007

view this page in

explode

(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 参数之前才行。

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

Example#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// *

?>

Example#2 limit 参数例子

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

// 正数的 limit
print_r(explode('|'$str2));

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

上例将输出:

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

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

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



fprintf> <echo
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
explode
Nobody
16-Nov-2008 05:38
A really better and shorter way to get extension is via:

<?php $extension = end(explode('.', $filename)); ?>

this will print the last part after the last dot :)
shaun
29-Aug-2008 12:24
For anyone trying to get an array of key => value pairs from a query string, use parse_str.  (Better alternative than the explode_assoc function listed way down the page unless you need different separators.)
pinkgothic at gmail dot com
15-Oct-2007 02:26
coroa at cosmo-genics dot com mentioned using preg_split() instead of explode() when you have multiple delimiters in your text and don't want your result array cluttered with empty elements. While that certainly works, it means you need to know your way around regular expressions... and, as it turns out, it is slower than its alternative. Specifically, you can cut execution time roughly in half if you use array_filter(explode(...)) instead.

Benchmarks (using 'too many spaces'):
Looped 100000 times:
preg_split: 1.61789011955 seconds
filter-explode: 0.916578054428 seconds

Looped 10000 times:
preg_split: 0.162719011307 seconds
filter-explode: 0.0918920040131 seconds

(The relation is, evidently, pretty linear.)

Note: Adding array_values() to the filter-explode combination, to avoid having those oft-feared 'holes' in your array, doesn't remove the benefit, either. (For scale - the '9' becomes a '11' in the benchmarks above.)

Also note: I haven't tested anything other than the example with spaces - since djogo_curl at yahoo's note seems to imply that explode() might get slow with longer delimiters, I expect this would be the case here, too.

I hope this helps someone. :)
seventoes at gmail dot com
09-Dec-2006 07:49
Note that explode, split, and functions like it, can accept more than a single character for the delimiter.

<?php
$string
= "Something--next--something else--next--one more";

print_r(explode('--next--',$string));
?>
djogo_curl at yahoo
01-Dec-2004 04: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.
coroa at cosmo-genics dot com
16-Nov-2003 08: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");

fprintf> <echo
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites