note 99830 deleted from function.explode by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:50:34 +0000 |
| Subject: | note 99830 deleted from function.explode by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215348@lists.php.net to get a copy of this message | ||
Note Submitter: tiago dot dias at flow-me dot com
----
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
(
)