What is the best method in PHP to remove a specific line from a text file?

To remove a specific line from a text file in PHP, you can read the contents of the file into an array, remove the desired line from the array, and then write the updated array back to the file. This can be achieved by using functions like file(), array_splice(), and file_put_contents().

// Specify the file path
$file = 'example.txt';

// Read the file into an array
$lines = file($file);

// Remove the desired line (e.g., line 3)
$lineToRemove = 3;
unset($lines[$lineToRemove - 1]);

// Write the updated array back to the file
file_put_contents($file, implode('', $lines));