[thelist] PHP - insert text at specific place in file?

Brent Eades beades at almonte.com
Tue Feb 20 12:15:01 CST 2007

OK, here's what I'm aiming for. I have an input form which is meant to 
simultaneously write both an HTML 'news' file and an equivalent RSS 
file, using variables passed from the form.

I have the HTML part mostly figured out -- I can append new news items 
at the top of the file, where I want them.

But for the RSS file, new items would of course have to be inserted at a 
particular point in the file, specifically, immediately after the 
closing </channel> element. Here's a snippet showing what I mean:

print "<channel>";
print "</channel>";
   [want to insert new item here, e.g.:]
$item .= "<item rdf:about=\"http://foo/" . $newURL . "\">";
$item .= "<title>" . $newtitle . "</title>";
$item .= "</item>\n";
$fh = fopen('file.xml'...);

So what I need to do is open 'file.xml', find the </channel> element, 
and then insert a new <item> above any pre-existing <items>. Is there a 
way of doing something like this in PHP, perhaps using preg_match or 
something along those lines?

Brent Eades	
Almonte, Ontario

More information about the thelist mailing list