尝试使用PHP修改文件

尝试使用PHP修改文件,php,file-io,Php,File Io,函数remove_tags必须进入文件并删除。但是,如果不覆盖整个文件,我似乎无法让它正常工作 <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

函数remove_tags必须进入文件并删除
。但是,如果不覆盖整个文件,我似乎无法让它正常工作

<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>

根据,您的str\u替换行应为:

$lines = str_replace("</channel></rss>", " ", $lines);
根据,您的str_替换线应为:

$lines = str_replace("</channel></rss>", " ", $lines);
你需要更换

str_replace("</channel></rss>", " ", $lines);
stru替换(“,”,$行);

$lines=str\u replace(“,”,$lines);
在“删除标签”功能中,您需要更换

str_replace("</channel></rss>", " ", $lines);
stru替换(“,”,$行);

$lines=str\u replace(“,”,$lines);

在“删除标签”功能中

感谢您发现其他问题!对php还是很陌生,所以我犯了一些愚蠢的小错误:)谢谢你发现了另一个问题!对php还是很陌生,所以我犯了一些愚蠢的小错误:)你说得对:)但我接受Michael的解决方案,因为他发现了另一个bug,现在一切都正常了。感谢您的帮助+1您的回答是正确的:)但我接受Michael的解决方案,因为他发现了另一个bug,现在一切正常。感谢您的帮助+1您没有为此使用的任何原因?您没有为此使用的任何原因?