note 74871 added to function.fgetcsv
| From: | php-general at lists dot php dot net | Date: | Tue, 01 May 2007 21:07:13 +0000 |
| Subject: | note 74871 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126098@lists.php.net to get a copy of this message | ||
a flexible parser that can be used for csv or tsv (or any delimited flatfile data source).
<?php
/* assumes a single line of input; automatically determines the number of fields */
function parse_line($input_text, $delimiter = ',', $text_qualifier = '"') {
$text = trim($input_text);
if(is_string($delimiter) && is_string($text_qualifier)) {
$re_d = '\x' . dechex(ord($delimiter)); //format for regexp
$re_tq = '\x' . dechex(ord($text_qualifier)); //format for regexp
$fields = array();
$field_num = 0;
while(strlen($text) > 0) {
if($text{0} == $text_qualifier) {
preg_match('/^' . $re_tq . '((?:[^' . $re_tq . ']|(?<=\x5c)' .
$re_tq . ')*)' . $re_tq . $re_d . '?(.*)$/', $text, $matches);
$value = str_replace('\\' . $text_qualifier, $text_qualifier, $matches[1]);
$text = trim($matches[2]);
$fields[$field_num++] = $value;
} else {
preg_match('/^([^' . $re_d . ']*)' . $re_d . '?(.*)$/', $text,
$matches);
$value = $matches[1];
$text = trim($matches[2]);
$fields[$field_num++] = $value;
}
}
return $fields;
} else {
return false;
}
}
?>
----
Server IP: 87.124.64.96
Probable Submitter: 69.48.114.91
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/74871
Del: integrated -- https://master.php.net/note/delete/74871/integrated
Del: useless -- https://master.php.net/note/delete/74871/useless
Del: bad code -- https://master.php.net/note/delete/74871/bad+code
Del: spam -- https://master.php.net/note/delete/74871/spam
Del: non-english -- https://master.php.net/note/delete/74871/non-english
Del: in docs -- https://master.php.net/note/delete/74871/in+docs
Del: other reasons-- https://master.php.net/note/delete/74871
Reject -- https://master.php.net/note/reject/74871
Search -- https://master.php.net/manage/user-notes.php