PHP从文本文件中删除包含字符串的行 [英] PHP Remove line containing string from a text file

查看:290
本文介绍了PHP从文本文件中删除包含字符串的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Kate 
Johnny
Bill
Kermit

我希望能够将Bill放入一个字符串中,并从文件中删除Bill或变量中的任何内容以及随后的\r \\\
。这个变量总是包含已经存在的文件,如果没有George,就不会有George。
这个代码检查文件是否是Bill。

  $ fileContents = file_get_contents(names.txt); 
if(strpos($ fileContents,Bill))
{
// Bill在这里!

$ / code>

我如何扩展这个去除Bill \\\
?提前感谢!

解决方案

  $ var =Bill; 
$ input =Kate
Johnny
Bill
Kermit;

$ output = str_replace($ var。\r\\\
,,$ input。\r\\\
);


I have a file that looks something like this.

Kate
Johnny
Bill
Kermit

I want to be able to put, for example, "Bill" into a string, and remove "Bill", or whatever is in the variable, and the subsequent "\r\n" from the file. The variable will always contain something that is already in the file, there won't be "George" if there is no George.
This code checks if "Bill" is in file.

$fileContents = file_get_contents("names.txt");
if(strpos($fileContents, "Bill"))
{
    //Bill is here!
}

How would I expand upon this to remove "Bill\r\n"? Thanks in advance!

解决方案

$var = "Bill";
$input = "Kate
Johnny
Bill
Kermit";

$output = str_replace($var ."\r\n", "", $input ."\r\n");

这篇关于PHP从文本文件中删除包含字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆