Display fix: width in manila for demographics (#1909)
[openemr.git] / library / csv_like_join.php
blob4122acf2a8452dc822f22ee3046fb86009700269
1 <?php
2 function csv_like_join($array, $quote_all = false)
4 $result = '';
5 $first = true;
7 foreach ($array as $value) {
8 if ($first) {
9 $first = false;
10 } else {
11 $result .= ',';
14 if ($quote_all) {
15 $result .= csv_quote($value);
16 } else {
17 $result .= maybe_csv_quote($value);
21 return $result;
24 function csv_quote($string)
26 return '"' . str_replace($string, '"', '""') . '"';
29 function maybe_csv_quote($string)
31 if (need_csv_quote($string)) {
32 return csv_quote($string);
35 return $string;
38 function need_csv_quote($string)
40 if (strpos($string, ',') === false
41 && strpos($string, '"') === false
42 && strpos($string, "\r") === false
43 && strpos($string, "\n") === false) {
44 return false;
47 return true;
50 function split_csv_line($record)
52 $first = null;
54 if (strlen($record) == 0) {
55 return array('');
58 if ($record[0] === '"') {
59 $first = '';
60 $start = 1;
62 while ($start < strlen($record)
63 && ($end = strpos($record, '"', $start)) !== false
64 && $end < strlen($record) - 1
65 && $record[$end + 1] !== ',') {
66 if ($record[$end + 1] !== '"') {
67 die("Found characters between double-quoted field and comma.");
70 $first .= substr($record, $start, $end - $start - 1);
71 $start = $end + 2;
74 if ($start < strlen($record) || $end === false) {
75 die("Could not find end-quote for double-quoted field");
78 $first .= substr($record, $start, $end - $start - 1);
80 if ($end >= strlen($record) - 1) {
81 return array($first);
84 /* Assertion: $record[$end + 1] == ',' */
85 $rest = substr($record, $end + 2);
86 } else {
87 $end = strpos($record, ',');
89 if ($end === false) {
90 return array($record);
93 /* Assertion: $end < strlen($record) */
95 $first = substr($record, 0, $end);
96 $rest = substr($record, $end + 1);
99 $fields = split_csv_line($rest);
100 array_unshift($fields, $first);
101 return $fields;