从文件中删除一行(如果存在)

时间:2017-05-18 19:45:11

标签: php file io

我已经习惯了PHP并尝试从文件中删除一行(如果存在)并重新保存文件。

所以,如果我有文件

user1
user2
user3
user4

我可以用

if(existsAndRemove("user3")){
    do thing
}

我尝试使用类似于下面代码的代码,但它有时会出错并且只会删除一行,如果它在文件中的最后一行。我不知道如何解决这个问题。

$data2 = file("./ats.txt");
 $out2 = array();
 foreach($data2 as $line2) {
     if(trim($line2) != $acc) {
         $out2[] = $line2;
     }
 }
 $fp2 = fopen("./ats.txt", "w+");
 flock($fp2, LOCK_EX);
 foreach($out2 as $line2) {
     fwrite($fp2, $line2);
 }
 flock($fp2, LOCK_UN);
 fclose($fp2);  
  }
}    

任何帮助都会非常感激,如果你能解释这些代码我也会很感激,所以我可以更容易地学习它! 谢谢。

3 个答案:

答案 0 :(得分:1)

如果文件大小足够小,你不担心将它全部读入内存,你可以做更多功能

    @model IEnumerable<Testy20161006.Controllers.CarModel>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index700</title>
    @*I had to add these references*@
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".getClick").click(function () {
                alert("ap");
                $(".clearMe").text("");
            })
        })
    </script>
</head>
<body>
    <table>
        @foreach (var car in Model)
        {
            <tr>
                <td>
                    @car.CarId
                </td>
                <td>
                    @car.CarMake
                </td>
                <td>
                    @car.theCarModel
                </td>
                <td>
                    @if (@car.CarId == 1)
                    {
                        <div id="result1" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else if (@car.CarId == 2)
                    {
                        <div id="result2" class="clearMe"></div> @*//update result based on ID*@
                    }
                    else
                    {
                        <div id="result3" class="clearMe"></div> @*//update result based on ID*@
                    }
                </td>
                <td>
                    Check for @Ajax.ActionLink(
                  @car.CarMake,
                               "getUpdate",
                               new { carId = car.CarId },
                               new AjaxOptions
                                {
                                    UpdateTargetId = "result" + car.CarId, //use car.ID here? not sure
                                    InsertionMode = InsertionMode.Replace,
                                    HttpMethod = "GET"
                                }, new { @class = "getClick" })
                </td>
            </tr>
        }
    </table>
</body>
</html>

答案 1 :(得分:0)

这样的事可能有用:

function remove_user($user) {
    $file_path = "foo.txt"
    $users = preg_split("[\n\r]+", file_get_contents($file_path));
    foreach ($users as $i => $existing) {
        if ($user == $existing) {
            $users = array_splice($users, $i, 1);
            file_put_contents($file_path, implode("\n", $users));
            break;
        }
    }
}

答案 2 :(得分:0)

因为您已经在使用file()

,所以应该会容易得多
$data2 = file("./ats.txt", FILE_IGNORE_NEW_LINES);
unset($data2[array_search('user3', $data2)]);
file_put_contents("./ats.txt", implode("\n", $data2));

或者首先检查它是否存在:

$data2 = file("./ats.txt", FILE_IGNORE_NEW_LINES);

if( ($key = array_search('user3', $data2)) !== false ) {
    unset($data2[$key]);
    file_put_contents("./ats.txt", implode("\n", $data2));
}