changes 0129092025 laptop
This commit is contained in:
@@ -62,4 +62,30 @@ public function detectColumnsFromCsv(string $path, bool $hasHeader): array
|
||||
|
||||
return [$bestDelim, $clean];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse columns from CSV using a specific delimiter. If $hasHeader is false,
|
||||
* returns positional indices instead of header names.
|
||||
*/
|
||||
public function parseColumnsFromCsv(string $path, string $delimiter, bool $hasHeader): array
|
||||
{
|
||||
$firstLine = $this->readFirstLine($path);
|
||||
if ($firstLine === null) {
|
||||
return [];
|
||||
}
|
||||
$row = str_getcsv($firstLine, $delimiter);
|
||||
$count = is_array($row) ? count($row) : 0;
|
||||
if ($hasHeader) {
|
||||
return array_map(function ($v) {
|
||||
$v = trim((string) $v);
|
||||
$v = preg_replace('/\s+/', ' ', $v);
|
||||
return $v;
|
||||
}, $row ?: []);
|
||||
}
|
||||
$cols = [];
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$cols[] = (string) $i;
|
||||
}
|
||||
return $cols;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user