希望得到一些援助PHP - 從平面文件讀取,刪除線和寫回平面文件
我有一個txt文件witht他以下內容:
1234|dog|apartment|two
1234|cat|apartment|one
1234|dog|house|two
1234|dog|apartment|three
我要刪除的條目,其中動物是居住在「房子」中的「狗」
<?php
if (isset($_POST['delete_entry]))
{
//identifies the file
$file = "db.txt";
//opens the file to read
@$fpo = fopen($file, 'r');
//while we have not reached the end of the file
while(!feof($fpo))
{
//read each line of the file into an array called animal
$animal[] = fgets($fpo);
}
//close the file
fclose($fpo);
//iterate through the array
foreach ($animal as $a)
{
if the string contains dog and apartment
if ((stripos ($a, 'dog']))&&(stripos ($a, 'house')))
{
//dont do anything
}
else
{
//otherwise print out the string
echo $a.'<br/>';
}
}
}
?>
這成功地打印出沒有「狗」和「房子」出現的條目的數組。 雖然我需要將它寫回平面文件,但遇到困難。
我已經嘗試了各種選項,包括立即發回每個條目時寫回文件。
Warning: feof() expects parameter 1 to be resource, boolean given in
Warning: fwrite(): 9 is not a valid stream resource in
Warning: fclose(): 9 is not a valid stream resource in
這些都是我遇到的錯誤。現在從我對數組的理解,當我通過這個名爲動物的數組,
- 它檢查索引[0]的兩個條件和
- 如果條目未找到,它分配給$ a。
- 然後它從索引[1],
開始經過數組 - 等等。
每次將新值分配給$ a。
我認爲,在打印每個看起來可能工作時間的文件,但是這是我得到的FWRITE及以上FCLOSE錯誤,不知道如何解決這個(還)。
我還是要做,我需要更換「公寓」有房子,一個專門所選條目的位,但會得到。有一次我已經整理出了「刪除」
我不需要的代碼,也許只是一個邏輯流程,可能會幫助我。
感謝
爲什麼不使用數據庫? – mosid 2013-05-30 12:24:24