note 72564 added to function.split
| From: | Theodule at osu1 dot php dot net | Date: | Wed, 24 Jan 2007 16:41:40 +0000 |
| Subject: | note 72564 added to function.split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122143@lists.php.net to get a copy of this message | ||
A little modification from dan jones.
_New_: I had a parameter to specify separator (default ",").
_Fix _: double-quotes who appear in a spreadsheet exported to CSV, they get escaped by doubling
them. So I remplace them by ' caracter
<?php
function getCSVValues($string,$separator=",")
{
$string = str_replace('""', "'", $string);
// split the string at double quotes "
$bits = explode('"',$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("/^".$separator."/","",$rest);
$rest = preg_replace("/".$separator."$/","",$rest);
$elements = array_merge($elements,explode($separator,$rest));
}
}
return $elements;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 195.6.25.115 (proxied: 152.12.50.145)
----
Manual Page -- http://www.php.net/manual/en/function.split.php
Edit -- https://master.php.net/note/edit/72564
Del: integrated -- https://master.php.net/note/delete/72564/integrated
Del: useless -- https://master.php.net/note/delete/72564/useless
Del: bad code -- https://master.php.net/note/delete/72564/bad+code
Del: spam -- https://master.php.net/note/delete/72564/spam
Del: non-english -- https://master.php.net/note/delete/72564/non-english
Del: in docs -- https://master.php.net/note/delete/72564/in+docs
Del: other reasons-- https://master.php.net/note/delete/72564
Reject -- https://master.php.net/note/reject/72564
Search -- https://master.php.net/manage/user-notes.php