Forum Moderators: coopster

Message Too Old, No Replies

PHP and XML - Line Breaks

         

username

3:26 am on May 13, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi, I am posting data from a text field, which needs to go into in a XML node from PHP. I am able to insert the data into the node with ease, but I am looking for the best way to handle line breaks. Basically if someone hits enter I want to maintain the consistency of the content for insertion into the XML node.

I have tried using CDATA unsuccessfully, entering <br /> tags on their own in the text seem to be ignored by the parser, so am not sure. There must be a tried and true method of including a paragraph of text and retrieving the data using PHP and XML?

Does anyone have any ideas or solutions to share with the community?

lorax

3:28 am on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I understand you correctly, you want the line breaks to show? If so, try nl2br(). If not, please explain further?

username

4:07 pm on May 13, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks Lorax.

When a user post the form, I want the following:

$text = $_POST['textfield']; //gets the exact entry from form

...then I want to insert it into and XML file using SimpleXML which I have achieved, but I want to make sure any new lines etc are maintained and the code is XHTML compliant for the insertion into the XML node. i.e.

$text = line01
line02
line03;

I need to be able to extract this from the xml. It is for a blog comments area. I need the comments to appear as they were entered, but just stored in xml, not a database.

Thanks.

eelixduppy

4:23 pm on May 13, 2008 (gmt 0)



You should use nl2br with htmlentities and then decode the string once you are ready for printing to the browser. As for an example, it could look something like this:

$text = $xml->addChild("text", [url=http://www.php.net/htmlentities]htmlentities[/url](nl2br($_POST['info'])));

And then when it comes to echoing, the following:


echo [url=http://www.php.net/html_entity_decode]html_entity_decode[/url]($xml->text[1]);

See how that works out for you :)

username

8:44 pm on May 13, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for your help. That worked really well. Even under obvious testing designed to break the application it stood up...nice work!

username

10:14 pm on May 13, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all,

I have noticed with the solution above, it seems that when I download the XML file from the server, it only displays half of the xml elements. They show up fine when I run the application, but when downloaded, they are all on the one line when viewed in a text editor, and stop half way through the document.

Is there an answer to this weirdness?

eelixduppy

10:41 pm on May 13, 2008 (gmt 0)



As far as them all being on one line is probably because when you are writing the new XML to the file you aren't adding any new line characters (\n) to the output, so it's going to write it all together like that. As far as not all the data being there, I really don't know what to say to do. It should be all there, especially if it is all there when you run your application.

username

2:41 am on May 14, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi eelixduppy...strangely enough when saving from the browser all the content is there, which I am willing to settle for, but to format the content for better viewing is hard because you have no control over what happens after the tag when using something like this:

$new->addChild('content', $content);

Is there a function to edit XML documents tag by tag with the ability to add a /n after a closing tag?

penders

4:38 pm on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...they are all on the one line when viewed in a text editor, and stop half way through the document.

Just a thought... may be your text editor has a limit to the length of line it will display? Word wrap ON?

username

4:53 pm on May 14, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



No, thanks, but it is not that. An example output of what is happening is:

<tags><node><nextnode>txt</nextnode><another></another>
</node></tags>

This all appears with no formatting, it would be nice to have:

<tags>
<node>
<nextnode>txt</nextnode>
<another></another>
</node>
</tags>