What is the best approach to search for and delete a specific line in a text file using PHP?

To search for and delete a specific line in a text file using PHP, you can read the contents of the file, search for the specific line, remove it from the array of lines, and then rewrite the modified content back to the file.

<?php
$filename = 'example.txt';
$line_to_delete = 'Specific line to delete';

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

// Search for the specific line and remove it
$index = array_search($line_to_delete, $lines);
if($index !== false) {
    unset($lines[$index]);
}

// Write the modified content back to the file
file_put_contents($filename, implode('', $lines));
?>