将不胜感激一些帮助
我有一个包含以下内容的 txt 文件:
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/>';
}
}
}
?>
这成功地打印出没有出现“dog”和“house”条目的数组。我需要把它写回平面文件,但遇到了困难。
我尝试了多种选择,包括在找到每个条目后立即写回文件。
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
这些是我遇到的错误之一。现在根据我对数组的理解,
- 当我遍历这个名为 animal 的数组时,
- 它会检查索引 [0] 的两个条件,并且
- 如果找不到该条目,它会分配给 $a。
- 然后它从索引 [1] 开始遍历数组,
- 以此类推。
每次将新值分配给 $a。
我认为每次出现时都将其打印到文件中可能会起作用,但这是我得到上述 fwrite 和 fclose 错误的地方,并且不知道如何解决这个问题(还)。
对于一个特别选择的条目,我仍然需要在需要用房子替换“公寓”的地方做一些事情,但是一旦我整理出“删除”,我就会到达那里
我不需要代码,也许只是一个可以帮助我的逻辑流程。
谢谢