note 11947 deleted from function.fgetcsv by spic
| From: | spic@php.net | Date: | Tue, 17 Sep 2002 09:23:04 +0000 |
| Subject: | note 11947 deleted from function.fgetcsv by spic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-36790@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);
}