Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using echo to export simple xml string adds an anomalous extra space

Status
Not open for further replies.

weedz

Programmer
Dec 5, 2000
718
0
0
NL
I have made a simple routine in which I build an xml string (no schema or validation).
After building the string I export it to the user using the echo function.
In my header I have the following:

<?php session_start(); ?>
<?php header("Content-Type: text/html; charset=utf-8");?>
<?php header("Content-Disposition:attachment; filename=veldcollection.xml");?>

and in the end I just do this:

echo $_xml;

This will export an xml file called veldcollection.xml.

The problem is, it will add an extra space at the beginning of the exported file, a space that is not at the beginning of the string.
Due to the space, the XML parser won't accept the xml file and will throw an error.
Removing the space manually after the file has been exported will make the file valid.

Can anyone tell me why this space is added and more importantly, how to get rid of it?

Thank you all in advance.

Weedz (Edward W.F. Veld)
 
try using just one php tag:

<?php
session_start();
php header("Content-Type: text/html; charset=utf-8");
php header("Content-Disposition:attachment; filename=veldcollection.xml");
?><xml START HERE>


Known is handfull, Unknown is worldfull
 
Some thoughts:
The extra space certainly happens after the PHP code which was shown. If there were a stray space before session_start() would produce an error message as headers cannot be sent since output already happened. Same for the two following header commands.
My best bet is that the code - which we don't see here - that creates the XML or the code immediately before the echo command is to blame.
 
>>Same for the two following header commands.

an "enter" is not considered then???

Known is handfull, Unknown is worldfull
 
vbkris
Interesting point.
Any character, any output (incl. \n, \r) before a header() command should produce the standard error message 'cannot send headers..." The script should fail (unless output buffering is on).
However, I don't know how the PHP intepreter handles the <??><??> tags when CR/LF are present.
 
Guys,

I have solved the problem.
First of all I changed the header:
text/html => text/xml

and removed an included php that had ob_start() in it, probably causing the extra space in the output.

Thanks for your help anyhow, much appreciated !

Weedz (Edward W.F. Veld)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top