3 function csv_like_join($array, $quote_all = false)
8 foreach ($array as $value) {
16 $result .= csv_quote($value);
18 $result .= maybe_csv_quote($value);
25 function csv_quote($string)
27 return '"' . str_replace($string, '"', '""') . '"';
30 function maybe_csv_quote($string)
32 if (need_csv_quote($string)) {
33 return csv_quote($string);
39 function need_csv_quote($string)
42 strpos($string, ',') === false
43 && strpos($string, '"') === false
44 && strpos($string, "\r") === false
45 && strpos($string, "\n") === false
53 function split_csv_line($record)
57 if (strlen($record) == 0) {
61 if ($record[0] === '"') {
66 $start < strlen($record)
67 && ($end = strpos($record, '"', $start)) !== false
68 && $end < strlen($record) - 1
69 && $record[$end +
1] !== ','
71 if ($record[$end +
1] !== '"') {
72 die("Found characters between double-quoted field and comma.");
75 $first .= substr($record, $start, $end - $start - 1);
79 if ($start < strlen($record) ||
$end === false) {
80 die("Could not find end-quote for double-quoted field");
83 $first .= substr($record, $start, $end - $start - 1);
85 if ($end >= strlen($record) - 1) {
89 /* Assertion: $record[$end + 1] == ',' */
90 $rest = substr($record, $end +
2);
92 $end = strpos($record, ',');
95 return array($record);
98 /* Assertion: $end < strlen($record) */
100 $first = substr($record, 0, $end);
101 $rest = substr($record, $end +
1);
104 $fields = split_csv_line($rest);
105 array_unshift($fields, $first);