Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly add newlines to an XML string in PHP?

Tags:

php

newline

xml

I have some xml that I manually construct in a function that is something like:

$xml = "<myxml>";
$xml .= "<items>";
$xml .= "<item1>blah</item1>";
$xml .= "</items>";
$xml .= "</myxml>";

When I output the string though I'd like the newlines to be present. How can I add this to the string without affecting a web service accepting the xml? Do newlines in XML even matter?

like image 375
stormist Avatar asked Dec 01 '22 05:12

stormist


1 Answers

The new lines aren't required but you can use the PHP_EOL constant which is a cross platform way of finding the new line character. i.e.

$xml = "<myxml>".PHP_EOL;
like image 70
Rwky Avatar answered Dec 04 '22 11:12

Rwky