如何在文件中查找与 HTML 输入匹配的单词,然后删除整个字符串


how to find a word in a file which matches the input from html, and then delete the entire string

`<?php
$aclname = $_POST['aclname'];
$file = file_get_contents("test.txt");
$lines = explode("'n", $file);
$exclude = array();
foreach ($lines as $line) {
    if (strpos($line, $aclname) !== FALSE) {
         continue;
    }
    $exclude[] = $line;
}
echo implode("'n", $exclude);
?>

请帮我提供打开文件的代码。在文件中查找与 html 中的输入变量匹配的单词,然后删除整个字符串。

$word = $_POST["字"];

应找到文件中$word的可能匹配项,并应删除整个字符串。

例:

输入

$word = hello

文件中的字符串

hello world
hi world
how are you world.

输出

hi world
how are you world

您将需要读取该文件的全部内容

$str=file_get_contents('file.txt');

然后替换要删除的邮件

//replace something in the file string - this is a VERY simple example
$str=str_replace("$oldMessage", "$deletedFormat",$str);

然后将其写在文件中

//write the entire string
file_put_contents('file.txt', $str);
您可以使用

preg_replace来替换值。

    $word = $_POST['word'];   
    $data = file_get_contents($filename);
    $data = preg_replace($word, '', $data);
    file_put_contents($filename, $data);