Project

General

Profile

Task #103099 ยป removeFieldFromCsv.php

Christian Kuhn, 2024-02-11 07:13

 
<?php
declare(strict_types=1);

$files = [];
foreach (glob($argv[1]) as $file) {
$files[] = $file;
}

foreach ($files as $file) {
$explodedContent = explode(chr(10), file_get_contents($file));
$isInTargetContext = false;
$newFileContent = [];
$fieldPosition = null;
foreach ($explodedContent as $line) {
if (!$isInTargetContext && str_starts_with($line, '"tx_testdatahandler_element",')) {
$isInTargetContext = true;
$newFileContent[] = $line;
continue;
}
if ($isInTargetContext && !str_starts_with($line, ',')) {
$isInTargetContext = false;
$fieldPosition = null;
$newFileContent[] = $line;
continue;
}
if ($isInTargetContext && $fieldPosition === null && str_contains($line, ',"t3_origuid",')) {
$explodedLine = explode(',', $line);
foreach ($explodedLine as $field => $fieldContent) {
if ($fieldContent === '"t3_origuid"') {
$fieldPosition = $field;
break;
}
}
if ($fieldPosition === null) {
throw new \LogicException('broken');
}
unset($explodedLine[$fieldPosition]);
$newFileContent[] = implode(',', $explodedLine);
continue;
}
if ($isInTargetContext && $fieldPosition !== null) {
$explodedLine = explode(',', $line);
unset($explodedLine[$fieldPosition]);
$newFileContent[] = implode(',', $explodedLine);
continue;
}
$newFileContent[] = $line;
}
$newFileContent = implode(chr(10), $newFileContent);
file_put_contents($file, $newFileContent);
}
    (1-1/1)