Writing to a File With Delimeters in Between Variables

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
yortzec
Forum Newbie
Posts: 2
Joined: Sun Jan 24, 2010 5:08 pm

Writing to a File With Delimeters in Between Variables

Post by yortzec »

I need some help here. I am trying to use php to write to an rss file (Basically a Unicode-8 text file with extension ".rss") when a person writes a post on my website. I used a php script that I downloaded to set up several things for me, so I already have all the variables i will need. They are: $name, $comments, and $added, representing the title of an rss article, the article itself, and the date the article was written, respectively. I also know where to put the function to write to the file. (But I don't know what to put in it) I want to make the function not replace everything in the rss file i am writing to. There will have to be more text above where I want to write in the rss file, and a static number of characters below it. (To be exact, the text I want to put in must be 16 characters before the end of the document, but I can always add more returns as "buffer" characters, to be sure that the code won't be placed in the wrong place) I also will need to put certain text in between the characters, and you will get the idea in the code below.

This is what I want to be written in the file:

Code: Select all

Here will be lots of text giving information about the rss feed, which I will never need to change. (I found it unnecessary to post them here)
 
--Earlier articles might be here
 
<item xmlns:atom="http://www.w3.org/2005/Atom">
<title xmlns:atom="http://www.w3.org/2005/Atom">[b]$name[/b]</title>
<link xmlns:atom="http://www.w3.org/2005/Atom"> http://simh.host56.com/simh/gbook/gbook.php</link>
<guid xmlns:atom="http://www.w3.org/2005/Atom"> http://simh.host56.com/simh/gbook/gbook.php</guid>
<description xmlns:atom="http://www.w3.org/2005/Atom">[b]$comments[/b]</description>
<pubDate xmlns:atom="http://www.w3.org/2005/Atom">[b]$added[/b]</pubDate>
</item>
 
 
</channel>
</rss>
 
I hope you understand what I mean. Please ask if you need more information. Thanks so much for the help!

P.S. The "</channel></rss>" at the end of the code are the static 16 characters I was talking about.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Writing to a File With Delimeters in Between Variables

Post by tr0gd0rr »

in your function you can include a php file and use an output buffer:

Code: Select all

function writeRss($name, $comments, $added) {
  ob_start();
  include ('myTemplate.xml.php');
  $xml = ob_get_clean();
  return file_put_contents($filename, $xml);
}
Then in myTemplate.xml.php use the code you pasted but wrap the variables with a `php echo`

Code: Select all

...
<title xmlns:atom="http://www.w3.org/2005/Atom"><?php echo $name?></title>
...
Post Reply