【发布时间】:2013-11-18 12:22:49
【问题描述】:
我想检测文本文件中的单词是否存在,然后将其删除。所以,这是我的代码:
<?php
$search = $id;
$lines = file("./user/".$_GET['own'].".txt");
// Store true when the text is found
$found = false;
foreach($lines as $line)
{
if(strpos($line, $search) !== false)
{
$found = true;
// open to read and modify
$file = "./user/".$_GET['own'].".txt";
$fh = fopen($file, 'r+');
$data = fread($fh, filesize($file));
$new_data = str_replace($id."\n", "", $data);
fclose($fh);
// Open to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_data);
fclose($fh);
$status = "has been successfully deleted.";
}
}
// If the text was not found, show a message
if(!$found)
{
$status = "is not exist in your list.";
}
?>
我在几个小时前就得到了这个工作。我对我的脚本做了一些更改,但不知何故,它不再工作了。任何人都可以看穿代码并告诉我出了什么问题吗??
或者任何人都可以提供更简单的方法来做我想做的事吗?我的代码乱七八糟..
【问题讨论】:
标签: php