How can the file() function in PHP be optimized to handle special characters and formatting issues when reading from a text file?

When using the file() function in PHP to read from a text file, special characters and formatting issues can arise if the file is not properly encoded or formatted. To handle this, you can specify the encoding of the file using the third parameter of the file() function to ensure that special characters are read correctly. Additionally, you can use functions like utf8_encode() or utf8_decode() to handle any encoding issues.

$file_lines = file('example.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES, 'UTF-8');

foreach ($file_lines as $line) {
    echo utf8_encode($line) . "<br>";
}