fwrite() running htmlentities() on 'string' parameter?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
UncleNinja
Forum Newbie
Posts: 6
Joined: Tue Jul 06, 2010 1:30 pm
Location: Georgia, USA

fwrite() running htmlentities() on 'string' parameter?

Post by UncleNinja »

Simple question and hopefully a simple problem. :)
I'm trying to replace the information inside of an XML node (in an XML file, duh) with PHP5's filesystem functions and SimpleXML. However, PHP has decided to convert all the reserved HTML characters (<, >, &, etc) to their &whatever; equivalents!

Here's my XML file, named pages.xml:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<data type="pages">
	<page value="example" title="The first example">
		<![CDATA[Just an <b>example</b>...]]>
	</page>
	<page value="exampleTwo" title="The second example">
		<![CDATA[The <i>second</i> example!]]>
	</page>
</data>
So I'm using SimpleXML w/ PHP5 to change that XML like this:

Code: Select all

<?php
$xml = simplexml_load_file("pages.xml");
//This is what I'm changing the second node to:
$xml->page[1] = '<![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It\'s truly amazing & inspiring]]>';

$fileHandle = fopen("pages.xml", "w");
fwrite($fileHandle, $xml->asXML());
fclose($fileHandle);
?>
It works until I look at the XML file after I run the PHP script. It looks like this (note second page node):

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<data type="pages">
	<page value="example" title="The first example">
		<![CDATA[Just an <b>example</b>...]]>
	</page>
	<page value="exampleTwo" title="The second example">
		<![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It's truly amazing & inspiring]]>
	</page>
</data>
What the heck? I checked with some echo's and it turns out fwrite is doing that. The second page node should look like this (not the bizarre mangled version above):

Code: Select all

<page value="exampleTwo" title="The second example">
	<![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It's truly amazing & inspiring]]>
</page>
Am I doing something wrong here? :?
Thanks!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: fwrite() running htmlentities() on 'string' parameter?

Post by Jade »

You just have to decode them: http://www.php.net/manual/en/function.h ... decode.php

Code: Select all

<?php
$xml = simplexml_load_file("pages.xml");
//This is what I'm changing the second node to:
$xml->page[1] = '<![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It\'s truly amazing & inspiring]]>';

$fileHandle = fopen("pages.xml", "w");
fwrite($fileHandle, html_entity_decode($xml->asXML()));
fclose($fileHandle);
?>
User avatar
UncleNinja
Forum Newbie
Posts: 6
Joined: Tue Jul 06, 2010 1:30 pm
Location: Georgia, USA

Re: fwrite() running htmlentities() on 'string' parameter?

Post by UncleNinja »

Thanks! That worked! :D
I wouldn't have expected to have to do that. Thank you! :)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: fwrite() running htmlentities() on 'string' parameter?

Post by Jade »

I've never tried to use fwrite for something with html in it but my guess is that it does htmlentities by default to prevent system attacks when reading the file.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: fwrite() running htmlentities() on 'string' parameter?

Post by AbraCadaver »

It's actually a simplexml problem. To do much more with cdata you'll need to use domdocument instead.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
UncleNinja
Forum Newbie
Posts: 6
Joined: Tue Jul 06, 2010 1:30 pm
Location: Georgia, USA

Re: fwrite() running htmlentities() on 'string' parameter?

Post by UncleNinja »

I just realized that if I really wanted to have an HTML entity in there it would be converted to the actual character. This isn't a bad thing unless the contents of that XML file is actually going into an HTML page later on. Well it is going into an HTML page, so that workaround works, but it breaks the pages.

Even if I use asXML() to save it like this (using example in original post):

Code: Select all

<?php
$xml = simplexml_load_file("pages.xml");
//This is what I'm changing the second node to:
$xml->page[1] = '<![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It\'s truly amazing & inspiring]]>';

$xml->asXML("pages.xml");
?>
It still turns out like:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<data type="pages">
        <page value="example" title="The first example">
                <![CDATA[Just an <b>example</b>...]]>
        </page>
        <page value="exampleTwo" title="The second example">
                <![CDATA[Dude, you should visit <a href="http://example.com">example.com</a>. It's truly amazing & inspiring]]>
        </page>
</data>
Doesn't that defeat the point of having it as a SimpleXML function? If this is is a 'feature' it's worse than magic quotes because I can't figure out how to turn it off! :banghead:

EDIT
Sorry, didn't see AbraCadaver's post. Thanks (no sarcasm intended) - now I know it's not just me. :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: fwrite() running htmlentities() on 'string' parameter?

Post by AbraCadaver »

So, as I said you'll probably want to use domdocument. But to keep simplexml, just extend and use a little dom mojo:

Code: Select all

class SimpleXMLExtended extends SimpleXMLElement {   
	public function addCData($cdata){   
		$node= dom_import_simplexml($this);   
		$owner = $node->ownerDocument;   
		$node->appendChild($owner->createCDATASection($cdata));
	}   
}

$doc = new SimpleXMLExtended($xml);   
$element = $doc->addChild('something');   
$node = $element->addChild('child');   
$node->addCData('some text for cdata section');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
UncleNinja
Forum Newbie
Posts: 6
Joined: Tue Jul 06, 2010 1:30 pm
Location: Georgia, USA

Re: fwrite() running htmlentities() on 'string' parameter?

Post by UncleNinja »

Thanks! :)
Post Reply