note 99830 added to function.explode
| From: | tiago dot dias at flow-me dot com | Date: | Thu, 09 Sep 2010 10:40:14 +0000 |
| Subject: | note 99830 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172400@lists.php.net to get a copy of this message | ||
Beaware splitting empty strings.
<?php
$str = "";
$res = explode(",", $str);
print_r($res);
?>
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.
Array
(
[0] =>
)
To solve this, just use array_filter() without callback. Quoting manual page "If the callback
function is not supplied, array_filter() will remove all the entries of input that are equal to
FALSE.".
<?php
$str = "";
$res = array_filter(explode(",", $str));
print_r($res);
?>
Array
(
)
----
Server IP: 194.8.30.16
Probable Submitter: 85.139.242.226
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/99830
Del: integrated -- https://master.php.net/note/delete/99830/integrated
Del: useless -- https://master.php.net/note/delete/99830/useless
Del: bad code -- https://master.php.net/note/delete/99830/bad+code
Del: spam -- https://master.php.net/note/delete/99830/spam
Del: non-english -- https://master.php.net/note/delete/99830/non-english
Del: in docs -- https://master.php.net/note/delete/99830/in+docs
Del: other reasons-- https://master.php.net/note/delete/99830
Reject -- https://master.php.net/note/reject/99830
Search -- https://master.php.net/manage/user-notes.php