【问题标题】:delete a line (.txt-File) containing something - with php删除包含某些内容的行(.txt-File) - 使用 php
【发布时间】:2021-10-20 04:31:18
【问题描述】:

我想删除包含$_POST['link'] 给出的内容的行

它已经适用于 .csv 文件。此时,.txt-File 的代码没有任何作用。

我的 php 代码

//txt
$txt = file('../textFiles/websites.txt');
$fpTXT = fopen('../textFiles/websites.txt', 'w');
foreach ($txt as $lines) {
  if (!str_contains($lines[2], strval($_POST['link']))) {
    fwrite($fpTXT, $lines);
  }
}
fclose($fpTXT);

用js触发

<!-- Delete-Function -->
    <script>
        async function deleteRequest(elem) {
            $.ajax({
                type: 'POST',
                url: '/sites/deleting.php',
                dataType: 'html',
                data: {
                    'link': elem
                }
            });
            location.reload();
        };
    </script>
</head>


<!-- Delete Icon -->
    <?php $delBtn = "<td><button class='delete' onclick=\"deleteRequest(this.parentNode.parentNode.getElementsByTagName('td')[2].innerText)\"><i class='material-icons'>&#xE872;</i></button></td>"; ?>

csv 代码(在 txt 代码上方)

//CSV
$csv = array_map('str_getcsv', file('../textFiles/websites.csv'));
$fpCSV = fopen('../textFiles/websites.csv', 'w');
foreach ($csv as $fields) {
  if (!str_contains($fields[2], strval($_POST['link']))) {
    fputcsv($fpCSV, $fields);
  }
}
fclose($fpCSV);

.txt 文件中的 2 行

【问题讨论】:

  • 你有没有调试过$lines[2]的值,看看有没有$_POST['link']
  • @rickdenhaan 输出只是我要删除的行的第二个字母的 6 倍(文件中的行数)
  • 呃,你要删除行的第二个字母吗?也许您最好在问题中包含一个包含该文件中两行的示例,以及一个发布值的实例。
  • 不,实际上是特定的行

标签: php txt


【解决方案1】:

您从file() 获得的“$lines”与 CSV 记录不同。每一个都是一行,所以$lines[2] 指的是该行的第三个字符:

$lines = "https://www.google.com\n";

$lines[2] is just a "t".

在 CSV 记录中,您将拥有

$lines = [ "A1", "B1", "C1", "D1" ]

and here, $lines[2] is "C1".

您应该过滤文本行以删除您不想要的行

$outputFile = '../textFiles/websites.txt';

$remove = strval($_POST['link']);
$txt = array_filter(
    file($outputFile),
    function ($eachLine) use ($remove) {
        return !str_contains($eachLine, $remove);
        // or equivalently,
        // return false === strpos($eachLine, $remove)
        // In all cases, consider that if someone sends ".",
        // you will remove ALL sites, since they'll all have a dot in their site name.
        // So maybe is better
        // return $remove !== trim($eachLine)
        // (the "trim" is needed to get the carriage return at the end of line out of the comparison, which otherwise would always fail).
    }
);

// Now you need to write the cleaned file
file_put_contents($outputFile, implode('', $txt));

【讨论】:

  • 非常感谢 :) 帮我解决了
  • @MtoseD 请记住检查我在函数中评论的那些边缘情况。单独使用“str_contains”可能会导致意外和不需要的结果。
【解决方案2】:

如果您只想替换文件中的单词或句子,您可以使用str_replace 轻松完成此操作:

$file = '../textFiles/websites.txt';
$findText = $_POST['link'];
file_put_contents($file, str_replace($findText, '', file_get_contents($file)));

【讨论】:

  • 请注意,这样,您将在文本文件中创建一个“间隙”; $findText 应该还包括文件分隔符(“\n”或“\r\n”)。这也将避免边缘情况 - 做作,但仍然 - 您要求删除“mail.google.com”并且您无意中将“mail.google.compressor.net”转换为“pressor.net”。但是,+1 是为了提高效率。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
相关资源
最近更新 更多