【发布时间】:2020-04-03 19:27:31
【问题描述】:
我有一个包含以下 2 行的文本文件
woof
woof99
然后我有这段代码,它应该采用用户名,例如 woof 并将其取出并用任何内容替换它。
$file = fopen("../UsersProfile/".$MyUsername."/otherfiles/Following.txt","a") or die("Unable to open file");
$content = file_get_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt");
$newcontent = str_replace($Username."\n", '', "$content");
file_put_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt", "$newcontent");
fclose($file);
文本文件现在看起来像这样,只有一行。
woof99
我遇到的问题是它由于某种原因无法正常工作。我相信它以前有效,但由于某种原因现在它不是。有没有更简单的方法来做到这一点? 谢谢!
编辑:我发现我需要做什么我需要做什么\r\n
【问题讨论】:
-
您是否打开了错误记录?您是否确认
file_get_contents()成功,即不返回FALSE?您是否确认file_put_contents()没有返回FALSE? -
file_get_contents()正在返回数据,file_put_contents 正在工作,问题是 str_replace
标签: php file fopen str-replace