1

The function remove_tags has to go into the file and remove </channel></rss>. However I can't seem to get it to work without overwriting the whole file.

<html><body><?php
$file_name = "rss.xml";

if (!file_exists($file_name)) {
        initialize_xml($file_name);
}

remove_tags($file_name);
write_content($file_name);
close_tags($file_name);
finish();

function initialize_xml($name) {
        $rss = fopen($name, 'w+') or die('can\'t open file_init');
        fwrite($rss, "<?xml version=\"1.0\" ?>\n");
        fwrite($rss, "<rss version=\"2.0\">\n");
        fwrite($rss, "<channel>\n");
        fwrite($rss, "<title>CBS IT Update Feed</title>\n");
        fwrite($rss, "<description>This feed will keep users up to date on IT issues that may arise</description>\n");
        fwrite($rss, "<link>http://google.com</link>\n");
        fwrite($rss, "<managingEditor>max.mackie@blood.ca</managingEditor>\n");
        fwrite($rss, "<webMaster>max.mackie@blood.ca</webMaster>\n\n");
        fwrite($rss, "</channel></rss>");
        fclose($rss);
}

function write_content($name) {
        $rss = fopen($name, 'a') or die('can\'t open file_write');
        fwrite($rss, "<item>\n");
        fwrite($rss, "<title>");
        fwrite($rss, $_POST['title']);
        fwrite($rss, "</title>\n");

        fwrite($rss, "<description><![CDATA[");
        fwrite($rss, $_POST['desc']);
        fwrite($rss, "]]></description>\n");

        fwrite($rss, "<date>");
        $today = getdate();
        $timestamp_format = $today['weekday'] . ' ' . $today['month'] . ' ' . $today['mday'] . ' ' . $today['hours'] . ' ' . $today['minutes'] . ' ' . $today['seconds'];
        fwrite($rss, $timestamp_format);
        fwrite($rss, "</date>\n");
        fwrite($rss, "</item>\n\n");
        fclose($rss);
}

function close_tags($name) {
        $rss = fopen($name, 'a') or die('can\'t open file_close');
        fwrite($rss, "</channel></rss>");
        fclose($rss);
}

function remove_tags($name) {
        $lines = file_get_contents('$name');
        str_replace("</channel></rss>", " ", $lines);
        $rss = fopen($name, 'w') or die('can\'t open file_remove');
        fwrite($rss, $lines);
}

function finish() {
        echo "The article <i> ";
        echo $_POST['title'];
        echo "</i>  has been added to the feed.<br>";
        echo "<a href=\"index.html\">Go Back</a> or <a href=\"rss.xml\">View the Feed</a>";
}
?>
</body></html>
  • any reason you are not using an [xml parser/writer](http://stackoverflow.com/questions/188414/best-xml-parser-for-php/3616044#3616044) for this? – Gordon May 13 '11 at 21:17

2 Answers2

2

According to the docs on str_replace, your str_replace line should be:

$lines = str_replace("</channel></rss>", " ", $lines);

Also, your file_get_contents call in remove_tags is reading a non existant file because of quotes (which is why $lines is empty when you write it back to the file). That line should look like so:

$lines = file_get_contents($name);
Michael Pryor
  • 25,046
  • 18
  • 72
  • 90
  • Thanks for spotting that other issue! Still VERY new to php so I make stupid little mistakes :) –  May 13 '11 at 20:02
2

you need to replace

str_replace("</channel></rss>", " ", $lines);

with

$lines = str_replace("</channel></rss>", " ", $lines);

in the remove_tags function

  • You were right with this :) But I'm accepting Michael's solutions because he found another bug and now everything works. Thanks for your help +1 –  May 13 '11 at 20:03