Php 如何在文件中找到与html输入匹配的单词,然后删除整个字符串
文件中的字符串Php 如何在文件中找到与html输入匹配的单词,然后删除整个字符串,php,html,Php,Html,文件中的字符串 $word = hello hello world hi world how are you world. 输出 $word = hello hello world hi world how are you world. 您可以使用preg\u replace替换值 hi world how are you world 您需要读取该文件的全部内容 $word = $_POST['word']; $data = file_get_conte
$word = hello
hello world
hi world
how are you world.
输出
$word = hello
hello world
hi world
how are you world.
您可以使用
preg\u replace
替换值
hi world
how are you world
您需要读取该文件的全部内容
$word = $_POST['word'];
$data = file_get_contents($filename);
$data = preg_replace($word, '', $data);
file_put_contents($filename, $data);
然后替换要删除的邮件
$str=file_get_contents('file.txt');
然后把它写在文件上
//replace something in the file string - this is a VERY simple example
$str=str_replace("$oldMessage", "$deletedFormat",$str);
欢迎来到stack?在你尝试之前是什么代码?嗨@A-2-A我已经在上面的问题中添加了代码,请帮助我删除行而不仅仅是单词。嗨。虽然我想把整条线都拆了。我也尝试了你的编码,不幸的是它没有工作。如果你能帮我解决这个问题,我将不胜感激。谢谢你的回复@urfusion-如果我可以删除整行和@saadeez,那就更好了-我不希望它被替换。请帮我想出一个解决办法。@lucky-不客气。在我的代码中,使用空引号代替变量
$deletedFormat
,现在您的代码将类似于$str=str\u replace(“$oldMessage”,“$deletedFormat”,$str)再次感谢,我希望程序能够理解接收到的输入,然后将其与一行中的一个单词进行匹配。如果匹配,则应删除该行。