I have a few XML templates that I am trying to fill using PHP's sprintf (v.5.3.8-Win54); however, I noticed that sprintf seems to be having problems with the < character. It seems that the < character needs to be followed by a space in order for sprintf to output it correctly. I looked at the PHP sprintf documentation but I couldn't find any references to any special character besides %. I echoed the following examples using echo sprintf($format1, $name, $age);
to show the problem:
$name = 'Lame';
$age = 1000;
$format1 = 'My name is %s. I am %d years old.';
$format2 = '<name> %s </name><age> %d </age>';
$format3 = 'name> %s /name>age> %d /age>';
$format4 = '<name %s </name<age %d </age';
$format5 = '<name> %s </name><age> %d </age>';
The formats are echoed as:
FORMAT 1: My name is Lame. I am 1000 years old.
FORMAT 2: Lame 1000
FORMAT 3: name> Lame /name>age> 1000 /age>
FORMAT 4:
FORMAT 5: <name> Lame </name><age> 1000 </age>
*FORMAT 4 does indeed come out as blank.
Any ideas why the < character isn't being output unless there is a space following it? I suppose I could just add a space to all the < characters in the XML templates using find/replace, but I rather find out why this is happening to begin with. I have a feeling this is going to be basic knowledge that I just haven't come across yet; if so, can you please provide references to any documentation?
INTERESTING: I had to add a space after the < character while typing out this question in the Stackoverflow interface so that it would appear correctly in the FORMAT 5 line! Huuuuuuh??!
* COMPLETE CODE *
<?php
$name = 'Lame';
$age = 1000;
$format1 = 'My name is %s. I am %d years old.';
$format2 = '<name> %s </name><age> %d </age>';
$format3 = 'name> %s /name>age> %d /age>';
$format4 = '<name %s </name<age %d </age';
$format5 = '< name> %s < /name>< age> %d < /age>';
echo 'FORMAT 1: ';
echo sprintf($format1, $name, $age);
echo '<br>';
echo 'FORMAT 2: ';
echo sprintf($format2, $name, $age);
echo '<br>';
echo 'FORMAT 3: ';
echo sprintf($format3, $name, $age);
echo '<br>';
echo 'FORMAT 4: ';
echo sprintf($format4, $name, $age);
echo '<br>';
echo '<br>';
echo 'FORMAT 5: ';
echo sprintf($format5, $name, $age);
?>