fix: ccda zip import and php warnings and deprecations (#7416)
[openemr.git] / library / csv_like_join.php
blob9d68386a5c478fdc26d64742b260c6c400a1488f
1 <?php
3 function csv_like_join($array, $quote_all = false)
5 $result = '';
6 $first = true;
8 foreach ($array as $value) {
9 if ($first) {
10 $first = false;
11 } else {
12 $result .= ',';
15 if ($quote_all) {
16 $result .= csv_quote($value);
17 } else {
18 $result .= maybe_csv_quote($value);
22 return $result;
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);
36 return $string;
39 function need_csv_quote($string)
41 if (
42 strpos($string, ',') === false
43 && strpos($string, '"') === false
44 && strpos($string, "\r") === false
45 && strpos($string, "\n") === false
46 ) {
47 return false;
50 return true;
53 function split_csv_line($record)
55 $first = null;
57 if (strlen($record) == 0) {
58 return array('');
61 if ($record[0] === '"') {
62 $first = '';
63 $start = 1;
65 while (
66 $start < strlen($record)
67 && ($end = strpos($record, '"', $start)) !== false
68 && $end < strlen($record) - 1
69 && $record[$end + 1] !== ','
70 ) {
71 if ($record[$end + 1] !== '"') {
72 die("Found characters between double-quoted field and comma.");
75 $first .= substr($record, $start, $end - $start - 1);
76 $start = $end + 2;
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) {
86 return array($first);
89 /* Assertion: $record[$end + 1] == ',' */
90 $rest = substr($record, $end + 2);
91 } else {
92 $end = strpos($record, ',');
94 if ($end === false) {
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);
106 return $fields;