This repository was archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.csvHelper.php
More file actions
133 lines (118 loc) · 3.84 KB
/
function.csvHelper.php
File metadata and controls
133 lines (118 loc) · 3.84 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Return two-dimensional array as CSV string.
*
* @param array $data array of associative arrays
* @return string CSV output or an empty string on error
*/
function arrayToCsv( array $data )
{
if( empty( $data ) )
{
return '';
}
/* display field/column names as first row */
$csvOutput = implode( ',', array_keys( current( $data ) ) ) . "\n";
/* get CSV log rows */
foreach( $data as $row )
{
/**
* Wrap strings in quotes and replaces mistyped tab- and newline-characters.
*
* @param string $str
*/
array_walk( $row, function( &$str ) {
if( !is_scalar( $str ) )
{
$str = implode( '|', $str );
}
$str = preg_replace(
array( "/\t/", "/\r?\n/", '/"/' ), array( "\\t", "\\n", '""' ), $str
);
$str = '"' . $str . '"';
} );
$csvOutput .= implode( ',', array_values( $row ) ) . "\n";
}
return $csvOutput;
}
/**
* Read a CSV file, given an open resource handle.
*
* @param resource $handle
* @param boolean $firstRowContainsHeadings Choose to either use the first row as headings or use no headings at all.
* @param int|boolean $identifierColumnNumber (optional) Zero-based number for the column that holds ID's (default: boolean FALSE to use the standard numeric index)
* @param array $overwriteHeadings (optional) The values will be used as headings. If there are fewer array entries than columns in the CSV, the rest will get numeric keys.
* @return array
*/
function readCsv( $handle, $firstRowContainsHeadings = true, $identifierColumnNumber = false, $overwriteHeadings = null )
{
/* alist of returned artist items */
$rows = array();
/* set up array label/table headers */
$header = array();
if( $firstRowContainsHeadings == true )
{
/* get CSV header (first line) */
$header = fgetcsv( $handle );
}
if( is_array( $overwriteHeadings ) && !empty( $overwriteHeadings ) )
{
/* set manual headings */
$header = $overwriteHeadings;
}
/* iterate over all rows in the CSV and put each row into an array entry */
while( ($data = fgetcsv( $handle )) !== false )
{
$columns = array();
foreach( $data as $i => $column )
{
$columns[(isset( $header[$i] ) ? $header[$i] : $i)] = $column;
}
if( $identifierColumnNumber === false )
{
$rows[] = $columns;
}
else
{
if( $data[$identifierColumnNumber] )
{
$rows[$data[$identifierColumnNumber]] = $columns;
}
}
}
return $rows;
}
/**
* Write data to a CSV file.
*
* @todo Do not use arrayToCsv() but write each line instead of a single string to be able to handle large amounts of data.
*
* @param array $data array of associative arrays
* @param string $filename The desired filename, a date will be appended by default
* @param string $path Will get used as path as output directory
* @param boolean $appendDate (optional) Toggles appending date string to filename (defaults to true)
* @return string Returns the filename that was written into or FALSE on error.
*/
function writeCsv( array $data, $filename, $path = null, $appendDate = true )
{
if( empty( $data ) )
{
return false;
}
/* display field/column names as first row */
$csvOutput = arrayToCsv( $data );
/* create path and filename */
if( $appendDate === true )
{
$filename = sprintf( '%s/%s.%s.csv', realpath( $path ), $filename, date( 'Y-m-d' ) );
}
else
{
$filename = sprintf( '%s/%s.csv', realpath( $path ), $filename );
}
if( false !== file_put_contents( $filename, $csvOutput ) )
{
return $filename;
}
return false;
}