What are the best practices for handling special characters like é in PHP when generating or manipulating XML files to avoid formatting errors?
Special characters like é can cause formatting errors in XML files if not properly handled. To avoid these issues, it's important to encode special characters using htmlspecialchars() or htmlentities() functions in PHP before generating or manipulating XML files. This ensures that the special characters are converted to their corresponding HTML entities, which XML can interpret correctly.
// Example of encoding special characters like é in PHP before generating XML
$text = "Café au lait";
$encoded_text = htmlspecialchars($text, ENT_XML1);
$xml = "<root><data>$encoded_text</data></root>";
echo $xml;