What are some best practices for handling character encoding issues when writing text files in PHP for further processing in applications like LaTeX?

Character encoding issues can arise when writing text files in PHP for further processing in applications like LaTeX. To ensure proper handling of character encoding, it is recommended to use UTF-8 encoding for both reading and writing files. This can be achieved by setting the appropriate encoding when opening the file and using functions like utf8_encode() or mb_convert_encoding() to convert the text to UTF-8 if needed.

// Open the file with UTF-8 encoding
$file = fopen('output.tex', 'w', 'UTF-8');

// Write text to the file
$text = "Some text with special characters like é and ü";
fwrite($file, $text);

// Close the file
fclose($file);