note 68961 added to function.split
| From: | dan dot jones at lunarfish dot co dot uk | Date: | Thu, 17 Aug 2006 12:24:04 +0000 |
| Subject: | note 68961 added to function.split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116004@lists.php.net to get a copy of this message | ||
Here's a function to split a string into csv values where they are optionally enclosed by
" to allow values with commas in.
I think it works. Let me know if I'm wrong.
Cheers. Dan
function getCSVValues($string) {
// split the string at double quotes "
$bits = split('"',$string);
$elements = array();
for ($i=0;$i<count($bits);$i++) {
/*
odd numbered elements would have been
enclosed by double quotes
even numbered elements would not have been
*/
if (($i%2) == 1) {
/* if the element number is odd add the
whole string to the output array */
$elements[] = $bits[$i];
} else {
/* otherwise split the unquoted stuff at commas
and add the elements to the array */
$rest = $bits[$i];
$rest = preg_replace("/^,/","",$rest);
$rest = preg_replace("/,$/","",$rest);
$elements = array_merge($elements,split(',',$rest));
}
}
return $elements;
}
----
Server IP: 83.138.144.80
Probable Submitter: 213.121.202.228
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.split.php
Edit -- https://master.php.net/note/edit/68961
Del: integrated -- https://master.php.net/note/delete/68961/integrated
Del: useless -- https://master.php.net/note/delete/68961/useless
Del: bad code -- https://master.php.net/note/delete/68961/bad+code
Del: spam -- https://master.php.net/note/delete/68961/spam
Del: non-english -- https://master.php.net/note/delete/68961/non-english
Del: in docs -- https://master.php.net/note/delete/68961/in+docs
Del: other reasons-- https://master.php.net/note/delete/68961
Reject -- https://master.php.net/note/reject/68961
Search -- https://master.php.net/manage/user-notes.php