@ tobylewis
No, it should not return a null array! The description clearly states: If delimiter contains a value that is not contained in string, then explode() will return an array containing string.
So it returns an array containing the original (empty) string.
Wouldn't you test for an invalid email address before trying to mail to it anyway? :S
explode
(PHP 4, PHP 5)
explode — Split a string by string
Description
array explode ( string $delimiter, 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 delimiter. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string, then explode() will return an array containing string.
If the limit parameter is negative, all components except the last -limit are returned. This feature was added in PHP 5.1.0.
Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
Note: The limit parameter was added in PHP 4.0.1
Example 2359. explode() examples
<?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// Example 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 2360. limit parameter examples
<?php
$str = 'one|two|three|four';
// positive limit
print_r(explode('|', $str, 2));
// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>
The above example will output:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Note: This function is binary-safe.
See also preg_split(), str_split(), strtok(), and implode().
explode
25-May-2007 02:49
25-May-2007 09:01
Watch out for this gottcha. Consider:
$arr = explode("/", "");
This should return a null array (ie count($arr) == 0).
Array
(
)
However, explode will instead return an array of one item which is a null string.
Array
(
[0] =>
)
There is some logic to the way this works but consider the following:
$addressees = "email@domain1.com, email@domain2.com";
$arr = explode(",", $addressees);
foreach($arr AS $to) mail ($to, $subject, $message);
with two items in the list it would sent two separate emails, with one it would sent one email message but with $addressees = "" it will still attempt to send one message that will fail because instead of returning an empty array explode returns an array with an empty item.
17-May-2007 03:45
@ JJ Rock, jason dot minett:
Here's an easy way around that:
<?php
$str = '^one^two^three^';
//Use trim() to remove extra delimiters
$arr = explode ('^', trim($str, '^'));
?>
26-Apr-2007 11:02
Just a quick note to compliment jason dot minett's comment a few down:
It's obvious that this works the opposite way as well:
<?php
$str = "^one^two^three";
$arr = explode ("^", $str);
?>
results in an empty value in $arr[0].
26-Apr-2007 09:08
<?php
// returns a string where $variables are replaced with their global value if available; removes all extra whitespaces
function evaluateString($string) {
if ($string) { // check for value
$array = explode(' ', $string); // split into parts
foreach ($array as $word) { // each part
if ($word[0] == '$') { // is part a variable
if ($word = substr($word, 1)) { // get variable name
global ${$word}; // retrieve global value
$html .= ${$word}; // add value to string
} // end variable name check
} else { // not a variable
$html .= $word; // add word to string
} // end variable check
$html .= ' '; // add space between words
} // end part loop
} // end string check
return trim($html); // trims final space from end
} // end evaluateString
?>
23-Apr-2007 01:43
of cause i ment the limit with my previouse post
@admin: wold u please change every "delimiter" in that post to "limit" and delete this note. thx.
22-Apr-2007 03:30
some more notes on the delimiter:
if the delimiter is 0, explode will return an array with one element containig the hole string (same as if the delimiter was 1).
if a negative delimiter is bigger or equal to the number of components, an empty array is returned.
<?php
print_r( explode( "|", "one|two|three|four", 0) );
print_r( explode( "|", "one|two|three|four", 1) );
?>
both print:
Array
(
[0] => one|two|tree|four
)
<?php
print_r( explode( "|", "one|two|three|four", -4) );
print_r( explode( "|", "one|two|three|four", -5) );
?>
both print:
Array
(
)
01-Mar-2007 04:09
A quick gotcha that had me head scratching for a while....
If the delimiter occurs right at the end of the string there will be an extra array element (an empty string):
<?php
$str = "aaa^elephant^chocolate^albatross^";
$arr = explode ("^", $str);
echo ("Array length: ".count($arr));
?>
---------------------------------
Array length: 5
27-Feb-2007 07:59
insensitive case explode function:
<?php
function iExplode($Delimiter, $String, $Limit = '')
{
$Explode = array();
$LastIni = 0;
$Count = 1;
if (is_numeric($Limit) == false)
$Limit = '';
while ( false !== ( $Ini = stripos($String, $Delimiter, $LastIni) ) && ($Count < $Limit || $Limit == ''))
{
$Explode[] = substr($String, $LastIni, $Ini-$LastIni);
$LastIni = $Ini+strlen($Delimiter);
$Count++;
}
$Explode[] = substr($String, $LastIni);
return $Explode;
}
?>
16-Jan-2007 10:03
I was using explode("\n", $string) to split a huge string (~ 0,5 MB). This took about 15 minutes to complete!
Using strtok($string, "\n") (followed by several thousand strtok("\n") of course), the same splitting is done in less than a minute.
17-Dec-2006 05:28
A 'between' function that we've all been waiting for. I am not savvy with regex so I resorted to explode();
<?php
function between($beg, $end, $str) {
$a = explode($beg, $str, 2);
$b = explode($end, $a[1]);
return $beg . $b[0] . $end;
}
echo between('<a>', '</a>', 'fsdfsdfsd<a>fsdfsd<a><a></a>sdfsdfsdf')
//<a>fsdfsd<a><a></a>
?>
10-Dec-2006 03: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));
?>
20-Oct-2006 06: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;
}
13-Mar-2006 06: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 . $pence\n";
01-Dec-2004 12: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.
24-Aug-2004 08: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.
16-Nov-2003 04: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");
