What are some common mistakes to avoid when searching for a specific string in a text file using PHP?

When searching for a specific string in a text file using PHP, common mistakes to avoid include not properly handling file opening and reading errors, not trimming the text input to remove extra whitespace, and not using the correct comparison operator when checking for the presence of the string.

// Open the text file for reading
$filename = 'example.txt';
$file = fopen($filename, 'r');

if ($file) {
    // Read the file line by line
    while ($line = fgets($file)) {
        // Trim the line to remove extra whitespace
        $trimmed_line = trim($line);
        
        // Check if the specific string is present in the line
        if (strpos($trimmed_line, 'specific string') !== false) {
            echo 'Found the specific string in the file!';
            break;
        }
    }
    
    // Close the file
    fclose($file);
} else {
    echo 'Error opening the file.';
}