What are some best practices for handling different character encoding formats in PHP when including external text files?

When including external text files in PHP, it's important to handle different character encoding formats properly to avoid issues with special characters or garbled text. One best practice is to detect the encoding of the file and convert it to UTF-8, which is widely supported. This can be done using functions like `mb_detect_encoding()` and `mb_convert_encoding()`.

// Read the contents of the file
$file_contents = file_get_contents('external_file.txt');

// Detect the encoding of the file
$encoding = mb_detect_encoding($file_contents);

// Convert the file contents to UTF-8 if it's not already in that format
if ($encoding != 'UTF-8') {
    $file_contents = mb_convert_encoding($file_contents, 'UTF-8', $encoding);
}

// Now you can use $file_contents safely in your PHP code