Since you are working with XML, you must also use one of the PHP XML extensions . The following example uses the DOM and XPath to find all text nodes in the XML document and trim.
Input signal:
$xml = <<< XML
<info>
<LastName>
HOOVER
</LastName>
</info>
XML;
The code:
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//text()') as $domText) {
$domText->data = trim($domText->nodeValue);
}
$dom->formatOutput = true;
echo $dom->saveXml();
Conclusion:
<?xml version="1.0"?>
<info>
<LastName>HOOVER</LastName>
</info>
source
share