How can i create a new line in an element?
I do:
$currentTrack->appendChild($domtree->createElement('code', ' test1;
test2;
test3;'));
However it adds
to the end of every line. How can i get rid of that?
is the Carriage Return part of a \r\n
style line ending. I think DOMDocument encodes it to preserve it. If you check the XML specification it says that it will get normalized to \n
if not encoded.
So you have different options:
\n
style line endings\n
before creating the DOMHere is some sample source to show the different behaviour:
$text = "test1;\r\ntest2;\r\ntest3;\r\n";
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->appendChild($root = $dom->createElement('root'));
$root->appendChild(
$node = $dom->createElement('code')
);
// text node - CR will get escaped
$node->appendChild($dom->createTextNode($text));
$root->appendChild(
$node = $dom->createElement('code')
);
// cdata - CR will not get escaped
$node->appendChild($dom->createCdataSection($text));
$root->appendChild(
$node = $dom->createElement('code')
);
// text node, CRLF and CR normalized to LF
$node->appendChild(
$dom->createTextNode(
str_replace(array("\r\n", "\r"), "\n", $text)
)
);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<code>test1;
test2;
test3;
</code>
<code><![CDATA[test1;
test2;
test3;
]]></code>
<code>test1;
test2;
test3;
</code>
</root>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With