★ wanayoo — archive 1999 http://www.php.net/manual/function.explode.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: explode
View the source code for this pageSearch the site



Previous page
 echo
 Updated
Sat, 12 Aug 2000
flush 
Next page


explode

(PHP3 , PHP4 )

explode -- Split a string by string

Description

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

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delim. If limit is set, the returned array will contaion a maximum of limit elements with the last element containing the whole rest of string.

Example 1. Explode() example


$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode (" ", $pizza);
      

See also split() and implode().


User Contributed Notes: explode


zak@nucleus.com
17-Feb-1999 06:07
If a null value ("") is passed to the _separator_ argument, then *explode* will not return a value.


If the _separator_ argument is passed a value that is not contained in the _string_ argument, then *explode* returns the value of the _string_ parameter.



boris@alum.mit.edu
15-Mar-1999 12:03
Be careful of initial, final or multiple adjacent occurrences of the separator: they will result in null values in the returned array:

explode(" ", " hello world")

returns a 4-value array, with elements "", "hello", "", and "world".



stuart@jhunix.hcf.jhu.edu
20-Aug-1999 03:48
Don't forget that strings are the same as character arrays, so you can reference them just like you do arrays. I spent an hour trying to find a version on the perl command @array = split( //, $str ) that would work in php3, but its just a waste of time :)


intrep@earthlink.net
13-Oct-1999 11:32
If you have a string seperated by nulls, one good way to explode () it is to use urlencode, for example:

<?
$newarray = explode ("%00", rawurlencode ($nullsepstr));
?>

This will explode your string properly.



paul@netkick.com
23-Jan-2000 11:51
Here's one way to explode under multiple conditions. I am developing a search module which will read a users search words seperated by commas and spaces.


function getKeywordsArray($keywords){

$keywords = ereg_replace(" ",",",$keywords);

$seperated = explode(",",$keywords);

return $seperated;

}




marcio@americas.bahiense.g12.br
23-Jan-2000 12:51
You can also do it using function split(), which receives reg exp. See the examples in function split().


cshenton@uucom.com
31-Jan-2000 07:12
if the $separator isn't found in the $string, then the
first element of the $array gets the value of the entire $string.



tanel.raja@tele2.ee
09-Mar-2000 10:05
A question that has been asked several times by beginners: Please note, that if there is a (white)space it ends up as the leading character of array element. There is big difference between

$item = "first string; 123"

and

$item = "first string;123"

To work properly you need to Trim() / LTrim() it away. However, pointing it out doesn't provide just-add-water solution, so I add a snippet of ready-to-use code derived from the another (non-working) snippet:

$itemlist = explode(";", $item);
//incorrect way
//$sql = "select * from <table> where <col>='$itemlist[0]' ";
//correct way
$sql = "select * from <table> where <col>='" . trim($itemlist[0]) . "' ";
mysql_db_query=(<dbase>,$sql);



stralud@libero.it
15-Mar-2000 11:41
ATTENTION!!
If you have:

$array = file("filename");

the last character of the array element is the newline char. So if you explode one of the array element you have to know that last element will have the newline char at the end.



joea@archtech.on.ca
29-Apr-2000 06:00
I was trying to add items from a file into a database, but there were commas in some of the records. I got it to work by:

while (!(feof($fp) ) ) {

$buffer = fgets($fp, 4096);

print "before: ".$buffer ;

$buffer = ereg_replace( '","' , '"|"' , $buffer);

$eachword = explode("|" , $buffer);

for ($k=0; $k<count($eachword); $k++) {

print "word $k =$eachword[$k]";

}



I am looking specificaly for the quote comma quote and changed it to something else for the explode to work.



bwuhlman@tallships.ca
31-Jul-2000 07:56
EXPLODE and SPLIT have the same idea, but they're not the same.

EXPLODE breaks up a string using another string as a delimiter, whereas SPLIT uses a regular expression. Thus, if your delimiter is a simple string, use explode. If its something more complicated that you can describe using a regex, then use split.


Thanks to the authors of the PHP manual from whom I borrowed that explanation almost word for word.



 About Notes


Previous page
 echo
 Updated
Sat, 12 Aug 2000
flush 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.