php - How can I remove unwanted spaces from txt file -
i'm trying modify txt file i'm using in dokuwiki.
i generate timestamp on top of txt file this:
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); }
and original txt file looks this:
now when use function:
$txt= "last generated: " . date("y m d h:i:s"); filecont($file,$txt);
i result this:
now don't want remove ====== open iot book ======
, it's because don't have empty space in first line?
but worst problem have is generates many empty spaces don't want.
i want last generated
@ top of txt file , else untouched
i tested code , removed newlines changing line:
$filecontents = file($file);
to
$filecontents = file($file, file_ignore_new_lines);
adding file_ignore_new_lines flag stops newline being added each element/line.
http://php.net/manual/en/function.file.php.
i removed array_unshift(), leaves '====== open iot book ======' in file.
so final function looked this:
function filecont($file,$data) { $filecontents = file($file, file_ignore_new_lines); //array_shift($filecontents); removed preserve '====== open iot book ======' line. array_unshift($filecontents, $data); $newcontent = implode("\n", $filecontents); $fp = fopen($file, "w+"); fclose($fp); }
Comments
Post a Comment