Skip to content

Instantly share code, notes, and snippets.

@ChristianGaertner
Created March 11, 2013 12:38
Show Gist options
  • Save ChristianGaertner/5133951 to your computer and use it in GitHub Desktop.
Save ChristianGaertner/5133951 to your computer and use it in GitHub Desktop.
This function just removes a specific line of a text document. Pass the path to the file or an URL to the file and the line you want to remove. The function will return the new file which you can save or work with it.
function removeLine ($file, $lineToRemove) {
$data = file_get_contents($file);
$lines = explode(PHP_EOL, $data);
$lineNo = 1;
foreach($lines as $line) {
$linesArray[$lineNo] = $line;
$lineNo++;
}
unset($linesArray[$lineToRemove]);
return implode("\n", $linesArray);
}
removeLine ("../dir/to/file.txt", 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment