What is the recommended method for escaping XML special characters in PHP, especially when dealing with Umlauts?

When dealing with XML special characters in PHP, including Umlauts, the recommended method for escaping them is to use the htmlspecialchars() function. This function will convert special characters like <, >, ", ', and & into their respective HTML entities, ensuring that the XML remains valid and properly encoded.

// Example code snippet to escape XML special characters, including Umlauts
$string = &quot;&Uuml;ml&auml;uts &amp; special &lt;characters&gt;&quot;;
$escaped_string = htmlspecialchars($string, ENT_XML1);
echo $escaped_string;