Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在文件中搜索word并删除行_Php - Fatal编程技术网

Php 在文件中搜索word并删除行

Php 在文件中搜索word并删除行,php,Php,这是关于同一主题的第二个请求。我不清楚 我需要删除这行 我在这里搜索并找到了脚本的一部分,该脚本是假定搜索 输入一个单词并删除该行。什么似乎有点小错误 我正在努力 我在下拉列表中有一个选项列表。我想让它 删除选定的行。调用的choice.php文件 从下拉页面上看,似乎是php发布时出现的 调用,因为没有拒绝访问或违反 错误 这些是我在添加最后3行之后得到的错误 有人告诉我需要 fopen() expects at least 2 parameters, 1 given implode(): I

这是关于同一主题的第二个请求。我不清楚 我需要删除这行

我在这里搜索并找到了脚本的一部分,该脚本是假定搜索 输入一个单词并删除该行。什么似乎有点小错误 我正在努力

我在下拉列表中有一个选项列表。我想让它 删除选定的行。调用的choice.php文件 从下拉页面上看,似乎是php发布时出现的 调用,因为没有拒绝访问或违反 错误

这些是我在添加最后3行之后得到的错误 有人告诉我需要

fopen() expects at least 2 parameters, 1 given
implode(): Invalid arguments passed
fwrite() expects parameter 1 to be resource, boolean given
fclose() expects parameter 1 to be resource, boolean given
提前谢谢

<?php
//  Separate choice.php has the following pull down 
//  Select item to delete from list
//  <option value="item1.php">Item 1</option>
//  <option value="item2.php">Item 2</option>
//  ...... many items.

$workitem = $_POST["itemtodelete"]; 

$file = file("option.list.php");
foreach( $file as $key=>$line ) {
    if( false !== strpos($line, $workitem) ) {
      unset ($file[$key]);
    }
}
// Removed "\n"
$file = implode("", $file);
// Told to add this.
$fp = fopen ("option.list.php");
fwrite($fp,implode("",$file);
fclose ($fp);

?>

fopen
需要一个
$mode
作为第二个参数,因此它会失败,并且需要
$fp
的一切都会失败

只需使用
文件内容
。它甚至会
内爆
阵列:

$workitem = $_POST["itemtodelete"]; 

$file = file("option.list.php");
foreach( $file as $key=>$line ) {
    if( false !== strpos($line, $workitem) ) {
      unset ($file[$key]);
    }
}

file_put_contents('option.list.php', $file);

嗯。您缺少一些右括号以及其他内容

$replaceItem = $_POST['itemtodelete']; // You should filter this data

$newContents = "";
$path = PATH_TO_FILE; // This could be hard coded, but not recommended
$filename = "option.list.php";

// Check to see if the file exists
if ( file_exists($path."/".$filename) ) {
    // Wrap our IO stuff so we catch any exceptions
    try {
        // Open the file for reading
        $fp = fopen($path."/".$filename, "r");
        if ($fp) {
            // Loop line-by-line through the file
            while($line = fgets($fp, 4096) !== false) {

                // Only add the line if it doesn't contain $replaceItem
                // This is case insensitive. I.E. 'item' == 'ITEM'
                // For case sensitive, use strstr()
                if ( stristr($line, $replaceItem) == false ) {
                    $newContents .= $line;
                }
            }
        }
        // Close our file
        fclose($fp);

        // Replace the contents of the file with the new contents
        file_put_contents($path."/".$filename, $newContents);                
    } catch (Exception $e) {
        throw new Exception($e->getMessage());
    }
}

编辑:试试这个。我对它做了一些修改。

因此,如果我理解,您希望删除文件
option.list.php
中包含
$workitem
值的行,对吗?这将替换字符串,而不是包含字符串的整行。@abracadver我知道。当我意识到他的问题是想删除这一行时,我修改了它。除非第一行项目被选中,否则它将删除所有项目。Thomas,它在您的while行中给出了语法错误。你没有完全固定它。很抱歉while行中的Extra')。还要确保该行为$newContents.=$line。.=将把字符串添加到现有字符串中。