2011-05-13 15 views
1

函數remove_tags必須進入文件並刪除</channel></rss>。但是,我似乎無法讓它在沒有覆蓋整個文件的情況下工作。試圖用PHP修改文件

<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>[email protected]</managingEditor>\n"); 
     fwrite($rss, "<webMaster>[email protected]</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> 
+0

任何原因,你不能使用[XML解析器/writer](http://stackoverflow.com/questions/188414/best-xml-parser-for-php/3616044#3616044)爲此? – Gordon 2011-05-13 21:17:11

回答

2

按照docs on str_replace,你str_replace函數行應該是:

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

而且,你的file_get_contents在remove_tags調用是閱讀,因爲行情的一個不存在的文件(這就是爲什麼$線是空當你把它寫回文件)。該行看起來應該像這樣:

$lines = file_get_contents($name); 
+0

感謝您發現其他問題!仍然非常新的PHP,所以我犯了愚蠢的小錯誤:) – n0pe 2011-05-13 20:02:36

2

需要更換

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

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

功能

+0

你對這個:)但我接受邁克爾的解決方案,因爲他發現了另一個錯誤,現在一切正常。謝謝你的幫助+1 – n0pe 2011-05-13 20:03:20

+0

是的,他有一個很好的眼睛。我完全錯過了它。 :) – exahex 2011-05-13 20:04:48