【问题标题】:PHP How to delete a specific line in a text file using user input? [duplicate]PHP 如何使用用户输入删除文本文件中的特定行? [复制]
【发布时间】:2023-03-19 19:22:01
【问题描述】:

用户从下拉列表中选择一个数字“$Num = $_POST['Number']”以从文本文件中删除一行。 例如。



鼠标

假设他们选择了第 3 行。如何删除文件中的行以便它只打印:


只使用他们选择的数字而不是行中的实际单词?

【问题讨论】:

  • 把你写的给我们

标签: php html


【解决方案1】:

使用这个:

$row_number = 0;    // Number of the line we are deleting
$file_out = file("file.txt"); // Read the whole file into an array

//Delete the recorded line
unset($file_out[$row_number]);

//Recorded in a file
file_put_contents("file.txt", implode("", $file_out));

【讨论】:

  • 将整个内容加载到内存中对于大文件来说并不是最佳选择
  • 是的,这是真的。但在上述问题中,据我了解,是关于少量数据的。
  • 这会从新文件中打印出所需的 2 行吗?因为我试图只使用 1 个文件,其中代码主动从文件中删除了该行,所以只保留了两行。
  • 效果很好。非常感谢:-)
  • 数据量大,文件大怎么办?
猜你喜欢
  • 1970-01-01
  • 2021-03-24
  • 2021-03-11
  • 1970-01-01
  • 2018-02-22
  • 2022-12-13
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多