Preserving format of xml file in textarea

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
jateeq
Forum Newbie
Posts: 14
Joined: Mon Mar 01, 2010 12:16 pm

Preserving format of xml file in textarea

Post by jateeq »

Hello,

I would like to open an xml file in php, and display the contents, without parsing the xml, in a textarea for editing. Currenlty, I am simply storing all lines in an array and listing the lines, but this messes up the formatting which is especially important with regards to xml files to promote user comprehension.

My code:

Code: Select all

 
$lines = file_get_contents($filelocation, 'r');
$content = file($filelocation, FILE_IGNORE_NEW_LINES);
$smarty-> assign('content', $content);
 
And then I go on to display each line in a smarty template. If you are not familiar with smarty, it doesn't matter, the result should be the same as echoing out the lines in php.

The following is a screenshot of what the file currently looks like. On the left is output using GeSHi, on the right is the output in the textarea using the aforementioned code. If I can make the latter look even remotely like the former, it'll help a lot.

[img]/home/jawadateeq/Desktop/Editing_session.png[/img]

Thanks,
Jawad.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Preserving format of xml file in textarea

Post by cpetercarter »

What does the second line of your code do? The file() function is presumably defined elsewhere in your code. In the code the second parameter is FILE_IGNORE_NEW_LINES, and my guess is that it does exactly what it says on the tin - it returns a string with new lines, tabs etc removed. What other options are available for the second parameter?

PS Sorry, I am being stupid. The php file() function reads a file into an array. A possible solution might be to remove the second parameter (FILE_IGNORE_NEW_LINES). Or, if you post the code of the relevant bit of your Smarty template, a different solution may occure to me.
Post Reply