Josh
Josh

Reputation: 1804

Using special characters when adding a child node with SimpleXML

I'm using SimpleXML to create an XML file that will be read by a flash game. The idea of the game is that there is a paragraph of text and within that text certain words will be hidden, and the user has to guess which word fits in that spot. The game wasn't created by me, so I don't have any way to edit it.

The XML file looks like this:

<page>
    <paragraph>
        All you have to do is <find>find</find> the missing words.
    </paragraph>
</page>

The word that is wrapped by the <find> tag is the word that is blanked out in the game, that the user has to guess what it is. You can have more than one hidden word to make the game harder.

My problem is, is that when I'm using this code (This is a simplified version, my version actually uses data from a form, but it's the same string being passed through):

$page->addChild('paragraph', 'All you have to do is <find>find</find> the missing words.');

The < and > are automatically escaped, I fully understand why they are, but I was wondering if there was any way to stop this from happening? Or if anyone had any idea how I could get around this?

Thanks.

Upvotes: 1

Views: 290

Answers (1)

ajreal
ajreal

Reputation: 47321

I don't think simplexml is capable for something like that,
how about DOMDocument?

$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML('<page/>');
$paragraph = $doc->createElement('paragraph');
$doc->appendChild($paragraph);

$paragraph->appendChild( new DOMText('All you have to do is ') );
$paragraph->appendChild( new DOMElement('find', 'find') );
$paragraph->appendChild( new DOMText(' the missing words.') );
echo $doc->saveXML();

Upvotes: 2

Related Questions