note 100264 added to function.str-getcsv

From: Date: Tue, 05 Oct 2010 04:28:34 +0000
Subject: note 100264 added to function.str-getcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-172960@lists.php.net to get a copy of this message
The replacement function for previous versions, written by hpartidas at deuz dot net, did function accurately for me. It can not seem to parse two commas with no space in between after handling an enclosure. I was forced to write my own. This is what I came up with. I hope you find it useful <?php if (!function_exists('str_getcsv')) { function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = '\\') { // Makes Sure we have correct input ELSE return false if (is_string($input) && !empty($input)) { $output = array(); // Initialize output array $enclosures = explode($enclosure, $input); // Split line by double quote to exempt "enclosed" delimiters // Now we run through array and separate remaining delimiters for($i=0; $i<count($enclosures); $i++) { // Even numbered array entries (0,2,4,...etc) become the areas outsite of the "quotes". if($i % 2 == 0) { // Make sure we dont allow a single delimiter to slip though as a segment. if($enclosures[$i] != $delimiter) { // Remove any delimiters other than those at the beginning of $input string. // These are delimiters which were used to separate our enclosures if( (substr($enclosures[$i], 0, 1) == $delimiter) && ($i != 0 ) ) { $enclosures[$i] = substr($enclosures[$i], 1); } // repeat this process for segment endings. Ignoring the last entry. if( (substr($enclosures[$i], -1, 1) == $delimiter) && ($i != count($enclosures)-1) ) { $enclosures[$i] = substr($enclosures[$i], 0, -1); } // Run a split on the cleaned segment to get the intended entries. $lines = preg_split("/".$delimiter."/", $enclosures[$i]); // Append each of thse entries to the output array. foreach($lines as $line) $output[] = $line; } } else { // Odd numbered segments are our enclosed areas and are saved as is because we want to keep these delimiters. $output[] = $enclosures[$i]; } } // finally return our output. return $output; } else { return false; } } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 68.2.198.205 ---- Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php Edit -- https://master.php.net/note/edit/100264 Del: integrated -- https://master.php.net/note/delete/100264/integrated Del: useless -- https://master.php.net/note/delete/100264/useless Del: bad code -- https://master.php.net/note/delete/100264/bad+code Del: spam -- https://master.php.net/note/delete/100264/spam Del: non-english -- https://master.php.net/note/delete/100264/non-english Del: in docs -- https://master.php.net/note/delete/100264/in+docs Del: other reasons-- https://master.php.net/note/delete/100264 Reject -- https://master.php.net/note/reject/100264 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172960) next »