|
1
|
|
|
<?php |
|
2
|
|
|
namespace AOE\Crawler\Utility; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
* |
|
16
|
|
|
* This file is a copy of the original to ensure compatibility for TYPO3 7, 8 and 9 at the same time |
|
17
|
|
|
* https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/core/Classes/Utility/CsvUtility.php |
|
18
|
|
|
* |
|
19
|
|
|
* TODO: This can be deleted/refactored when dropping support for TYPO3 7.6 |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class with helper functions for CSV handling |
|
25
|
|
|
*/ |
|
26
|
|
|
class CsvUtility |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Convert a string, formatted as CSV, into an multidimensional array |
|
30
|
|
|
* |
|
31
|
|
|
* This cannot be done by str_getcsv, since it's impossible to handle enclosed cells with a line feed in it |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $input The CSV input |
|
34
|
|
|
* @param string $fieldDelimiter The field delimiter |
|
35
|
|
|
* @param string $fieldEnclosure The field enclosure |
|
36
|
|
|
* @param int $maximumColumns The maximum amount of columns |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function csvToArray($input, $fieldDelimiter = ',', $fieldEnclosure = '"', $maximumColumns = 0) |
|
40
|
|
|
{ |
|
41
|
|
|
$multiArray = []; |
|
42
|
|
|
$maximumCellCount = 0; |
|
43
|
|
|
|
|
44
|
|
|
if (($handle = fopen('php://memory', 'r+')) !== false) { |
|
45
|
|
|
fwrite($handle, $input); |
|
46
|
|
|
rewind($handle); |
|
47
|
|
|
while (($cells = fgetcsv($handle, 0, $fieldDelimiter, $fieldEnclosure)) !== false) { |
|
48
|
|
|
$maximumCellCount = max(count($cells), $maximumCellCount); |
|
49
|
|
|
$multiArray[] = preg_replace('|<br */?>|i', LF, $cells); |
|
50
|
|
|
} |
|
51
|
|
|
fclose($handle); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($maximumColumns > $maximumCellCount) { |
|
55
|
|
|
$maximumCellCount = $maximumColumns; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($multiArray as &$row) { |
|
59
|
|
|
for ($key = 0; $key < $maximumCellCount; $key++) { |
|
60
|
|
|
if ( |
|
61
|
|
|
$maximumColumns > 0 |
|
62
|
|
|
&& $maximumColumns < $maximumCellCount |
|
63
|
|
|
&& $key >= $maximumColumns |
|
64
|
|
|
) { |
|
65
|
|
|
if (isset($row[$key])) { |
|
66
|
|
|
unset($row[$key]); |
|
67
|
|
|
} |
|
68
|
|
|
} elseif (!isset($row[$key])) { |
|
69
|
|
|
$row[$key] = ''; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $multiArray; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $row Input array of values |
|
81
|
|
|
* @param string $delim Delimited, default is comma |
|
82
|
|
|
* @param string $quote Quote-character to wrap around the values. |
|
83
|
|
|
* @return string A single line of CSV |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function csvValues(array $row, $delim = ',', $quote = '"') |
|
86
|
|
|
{ |
|
87
|
|
|
$out = []; |
|
88
|
|
|
foreach ($row as $value) { |
|
89
|
|
|
$out[] = str_replace($quote, $quote . $quote, $value); |
|
90
|
|
|
} |
|
91
|
|
|
return $quote . implode($quote . $delim . $quote, $out) . $quote; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|