希望得到一些援助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