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: Fri, 14 Nov 2008

view this page in

explode

(PHP 4, PHP 5)

explodeCoupe une chaîne en segments

Description

array explode ( string $delimiter , string $string [, int $limit ] )

Retourne un tableau de chaînes, chacune d'elle étant une sous-chaîne du paramètre string extraite en utilisant le séparateur delimiter .

Liste de paramètres

delimiter

Le séparateur.

string

La chaîne initiale.

limit

Si limit est défini, le tableau retourné contiendra, au maximum, limit éléments, dont le dernier élément contiendra le reste de la chaîne.

Si le paramètre limit est négatif, tous les éléments, excepté les -limit derniers éléments sont retournés.

Bien que implode() puisse, pour des raisons historiques, accepter ces paramètres dans n'importe quel ordre, explode() ne le peut pas. Vous devez vous assurer que le paramètre delimiter soit placé avant le paramètre string .

Valeurs de retour

Si delimiter est une chaîne vide (""), explode() retournera FALSE. Si delimiter contient une valeur qui n'est pas contenue dans string , alors explode() retournera un tableau, contenant la chaîne string entière.

Historique

Version Description
5.1.0 Le paramètre limit peut désormais être négatif
4.0.1 Le paramètre limit a été ajouté

Exemples

Exemple #1 Exemple avec explode()

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

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

?>

Exemple #2 Exemple avec le paramètre limit

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

// limit positif
print_r(explode('|'$str2));

// limit négatif (depuis PHP 5.1)
print_r(explode('|'$str, -1));
?>

L'exemple ci-dessus va afficher :

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

Notes

Note: Cette fonction gère les chaînes binaires.



fprintf> <echo
Last updated: Fri, 14 Nov 2008
 
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: Fri, 14 Nov 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites