On hosting there is a file representing the form:

abc | Text | Text cba | Text | Text cba | Text | Text 

You only need to compare the first column. And the question is, how can I re-delete the rows in which the first columns match?

  • Well, I think “regular expressions” will help you. For example, this is the link - Maxim Cherednik

2 answers 2

Another option in the collection of answers:

 $fn = 'file_name.txt'; // имя текст.файла $arr = file_exists($fn) ? file($fn, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) : []; if (! empty($arr)) { $tmp = []; $arr = array_filter($arr, function($i)use(&$tmp){ return (in_array(($e = explode('|', $i)[0]), $tmp)) ? '' : $tmp[] = $e; }); file_put_contents($fn, join("\n", $arr)); } 
     $inFile = fopen('myfile', 'r'); if ($inFile) { $outFile = fopen('myfile.tmp', 'wb'); if ($outFile) { $keys = []; while (!feof($inFile)) { $str = fgets($inFile); $strData = explode(' | ', $str); if (!isset($keys[$strData[0]])) { fwrite($outFile, $str . PHP_EOL); $keys[] = $strData[0]; } } fclose($outFile); } fclose($inFile); rename('myfile.tmp', 'myfile'); }