note 122690 deleted from function.str-getcsv by cmb
| From: | cmb@php.net | Date: | Sun, 09 Feb 2020 08:22:34 +0000 |
| Subject: | note 122690 deleted from function.str-getcsv by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214056@lists.php.net to get a copy of this message | ||
Note Submitter: jcours at NOSPAM dot numifinancial dot com
----
str_getcsv() trimmed its results in PHP 5.3. It does not trim in 5.6. So in 5.3.3,
str_getcsv("a, b") would yield
<?php
Array
(
[0] => a
[1] => b
)
?>
while in 5.6 you would get
<?php
Array
(
[0] => a
[1] => b
)
?>
If you pass str_getcsv() a non-string, it will print a warning and return NULL, so a more accurate
return value might be "array|NULL".
And finally, str_getcsv("") yields an array containing a single NULL.
Putting those behaviors together, you can emulate the old str_getcsv() behavior with a function like
this:
<?php
function str_trimgetcsv($line) {
$val = str_getcsv($line);
if (is_array($val)) {
$val = array_map(
function($a){return($a === NULL ? $a : trim($a));},
$val);
}
return $val;
}
?>