What are common pitfalls when trying to delete a specific line from a text file using PHP?

One common pitfall when trying to delete a specific line from a text file using PHP is not properly handling line endings, especially if the file was created on a different operating system. To solve this issue, you can read the file line by line, skip the line you want to delete, and then write the remaining lines to a new file.

<?php
$filename = 'example.txt';
$line_number = 3; // Line number to delete

$lines = file($filename);
$output = '';

foreach ($lines as $key => $line) {
    if ($key != $line_number - 1) {
        $output .= $line;
    }
}

file_put_contents('new_example.txt', $output);
?>