How can the use of DOMDocument in PHP help in removing line breaks from XML code for emails?

When sending XML code in emails, line breaks can cause formatting issues. To remove line breaks from XML code for emails in PHP, you can use the DOMDocument class to parse the XML and then save it as a string without line breaks.

// Load the XML content
$xml = '<root>
    <element1>Value 1</element1>
    <element2>Value 2</element2>
</root>';

$doc = new DOMDocument();
$doc->loadXML($xml);

// Remove line breaks
$doc->preserveWhiteSpace = false;
$doc->formatOutput = false;

// Get the XML content without line breaks
$cleanXml = $doc->saveXML();

// Use $cleanXml in your email content
echo $cleanXml;