FGetCSV.php
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
class File_FGetCSV {
function fgetcsv($f, $length, $d=",", $q='"') {
$list = array();
$st = fgets($f, $length);
if ($st === false || $st === null) return $st;
while ($st !== "" && $st !== false) {
if ($st[0] !== $q) {
# Non-quoted.
list ($field) = explode($d, $st, 2);
$st = substr($st, strlen($field)+strlen($d));
} else {
# Quoted field.
$st = substr($st, 1);
$field = "";
while (1) {
# Find until finishing quote (EXCLUDING) or eol (including)
preg_match("/^((?:[^$q]+|$q$q)*)/sx", $st, $p);
$part = $p[1];
$partlen = strlen($part);
$st = substr($st, strlen($p[0]));
$field .= str_replace($q.$q, $q, $part);
if (strlen($st) && $st[0] === $q) {
# Found finishing quote.
list ($dummy) = explode($d, $st, 2);
$st = substr($st, strlen($dummy)+strlen($d));
break;
} else {
# No finishing quote - newline.
$st = fgets($f, $length);
}
}
}
$list[] = $field;
}
return $list;
}
function fputcsv($f, $list, $d=",", $q='"') {
$line = "";
foreach ($list as $field) {
# remove any windows new lines,
# as they interfere with the parsing at the other end
$field = str_replace("\r\n", "\n", $field);
# if a deliminator char, a double quote char or a newline
# are in the field, add quotes
if(ereg("[$d$q\n\r]", $field)) {
$field = $q.str_replace($q, $q.$q, $field).$q;
}
$line .= $field.$d;
}
# strip the last deliminator
$line = substr($line, 0, -1);
# add the newline
$line .= "\n";
# we don't care if the file pointer is invalid,
# let fputs take care of it
return fputs($f, $line);
}
}
?>