note 11947 added to function.fgetcsv.php

From: Date: Wed, 14 Mar 2001 21:57:33 +0000
Subject: note 11947 added to function.fgetcsv.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-12404@lists.php.net to get a copy of this message
Following is a hack solution for those that do not have the latest and greatest... If you have a better solution for those of us that do not/cannot use fgetcsv(), please post here! BTW, if you view the souce of this page you can copy/paste the pretty version of this code. You'll need to remove the BRs tho. Enjoy! -Ken ######################################## function csv_parse ($data, $separator) { $quote = '"'; $values = array(); $toggle = 0; $len = strlen($data); $count = 1; for ($i = 0; $i < $len; $i++) { $tmp = substr ($data, $i, 1); if (strcmp($tmp, $quote) == 0) { $toggle = $toggle ^ 1; } $value = $value . $tmp; if (strcmp($tmp, $separator) == 0) { if (! $toggle) { # End of word $value = ereg_replace(",$", "", $value); $value = ereg_replace("^\"", "", $value); $value = ereg_replace("\"$", "", $value); $value = ereg_replace("\"+", "\"", $value); $num_of_elems = count($values); $values[$num_of_elems] = $value; $value = ""; } } } $value = ereg_replace("^\"", "", $value); $value = ereg_replace("\"$", "", $value); $value = ereg_replace("\"+", "\"", $value); $num_of_elems = count($values); $values[$num_of_elems] = $value; return ($values); } http://www.php.net/manual/en/function.fgetcsv.php

« previous php.notes (#12404) next »