ThunderAI Posted June 18, 2008 Share Posted June 18, 2008 How do you go about deleting the first x number of lines of a CSV. I want the first three or one line deleted. I could open the file and read the lines, but how do I physicaly delete them so they are not read by the LOAD DATA function. Link to comment https://forums.phpfreaks.com/topic/110808-solved-delete-x-lines-of-a-csv/ Share on other sites More sharing options...
sasa Posted June 18, 2008 Share Posted June 18, 2008 try <?php $fileName = 'test.dat'; $numLine = 3; $a = preg_replace("/^(.*?\n){0,$numLine}/",'', file_get_contents($fileName)); $b = fopen('test.dat', 'w'); fwrite($b, $a); fclose($b); ?> Link to comment https://forums.phpfreaks.com/topic/110808-solved-delete-x-lines-of-a-csv/#findComment-568597 Share on other sites More sharing options...
hitman6003 Posted June 19, 2008 Share Posted June 19, 2008 <?php $num_to_remove = 3; $file = file($filename); // php5 only file_put_contents($filename, implode("\n", array_slice($file, $num_to_remove))); // php4 /* $open = fopen($filename, 'w'); fwrite($open, implode("\n", array_slice($file, $num_to_remove))); fclose($open); */ Link to comment https://forums.phpfreaks.com/topic/110808-solved-delete-x-lines-of-a-csv/#findComment-568672 Share on other sites More sharing options...
ThunderAI Posted June 19, 2008 Author Share Posted June 19, 2008 First suggestions works 100% correctly. Second one looks cleaner, but adds an extra line space in between every row. Is there a way to remove that extra line space? Link to comment https://forums.phpfreaks.com/topic/110808-solved-delete-x-lines-of-a-csv/#findComment-569067 Share on other sites More sharing options...
hitman6003 Posted June 19, 2008 Share Posted June 19, 2008 Remove the implode function... file_put_contents($filename, array_slice($file, $num_to_remove)); Link to comment https://forums.phpfreaks.com/topic/110808-solved-delete-x-lines-of-a-csv/#findComment-569205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.