Php 如何从txt文件中删除不需要的空格
我正在尝试修改我在dokuwiki中使用的txt文件 我在txt文件上生成时间戳,如下所示:Php 如何从txt文件中删除不需要的空格,php,Php,我正在尝试修改我在dokuwiki中使用的txt文件 我在txt文件上生成时间戳,如下所示: function filecont($file,$data) { $fileContents = file($file); array_shift($fileContents); array_unshift($fileContents, $data); $newContent = implode("\n", $fileContents); $fp = fop
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
我的原始txt文件如下所示:
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
现在当我使用我的函数时:
$txt= "Last generated: " . date("Y M D h:i:s");
filecont($file,$txt);
我得到的结果如下:
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
现在我不想删除我的============================
,可能是因为我的第一行没有空白
但我遇到的最糟糕的问题是产生了很多我不想要的空空间
我只想在txt文件的顶部获取上次生成的
,,其他未触及的内容可能会删除这一行
array_shift($fileContents);
解决您的问题?可能只是删除这一行
array_shift($fileContents);
解决您的问题?当您获取文件元素时,您需要检查上次生成的
是否与第一行一致,您需要使用数组\u shift
$fileContents = file($file);
if(stripos($fileContents[0],"Last generated:") !== false)
{
array_shift($fileContents); //if found use shift
}
array_unshift($fileContents, $data);
当您获取文件元素时,您需要检查上次生成的
是否与第一行一致,您需要使用array\u shift
$fileContents = file($file);
if(stripos($fileContents[0],"Last generated:") !== false)
{
array_shift($fileContents); //if found use shift
}
array_unshift($fileContents, $data);
我测试了您的代码,并通过更改行删除了额外的换行:
$fileContents = file($file);
到
添加文件\忽略\新建\行标志将停止向每个元素/行添加新行
我还删除了数组_unshift(),该数组在文件中留下“==========Open IoT book========”
因此,我的最终函数如下所示:
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
我测试了您的代码,并通过更改行删除了额外的换行:
$fileContents = file($file);
到
添加文件\忽略\新建\行标志将停止向每个元素/行添加新行
我还删除了数组_unshift(),该数组在文件中留下“==========Open IoT book========”
因此,我的最终函数如下所示:
function filecont($file,$data)
{
$fileContents = file($file);
array_shift($fileContents);
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fputs($fp, $newContent);
fclose($fp);
}
function filecont($file,$data)
{
$fileContents = file($file, FILE_IGNORE_NEW_LINES);
//array_shift($fileContents); Removed to preserve '====== Open IoT book ======' line.
array_unshift($fileContents, $data);
$newContent = implode("\n", $fileContents);
$fp = fopen($file, "w+");
fclose($fp);
}
我以前试过了,但语法错误!不知道为什么我以前试过,但我有语法错误!不知道你为什么工作,谢谢。PS我甚至不知道有一个文件\u忽略\u新的\u行没有问题。很高兴我能帮上忙。工作了,谢谢。PS我甚至不知道有一个文件\u忽略\u新的\u行没有问题。很高兴我能帮忙。