i am using php to access an html file
$file = file("index.html");
then I am using
file_put_contents( "index.html", implode(PHP_EOL, $file) );
but what is going on here?!... there are new lines everywhere... the file is much bigger now, useless new line!!
if I do:
file_put_contents( "index.html", implode("", $file) );
the problem is fixed...
but if I modify the array $file
, anything I add will be on the same line. I cannot add new values. what is going on here?!
somehow the strings in the file have new lines encoded in them.... and any string I add does not.
example: a 10 line file will have a bunch of text. If I read the file and get an array of 10 values, then add 11... using implode("", $file) ); will give... 10 lines!
what happened to my 11th value? it will be on the same line!
how is this possible?! what is going on here?!
By default, file()
keeps the newlines in the strings that it returns in the array. You get double-spacing if you add another newline between each line with implode()
. And if you add additional elements to the array, but don't end them with a newline, you'll get inconsistent results.
You can use the FILE_IGNORE_NEW_LINES
flag to tell it to strip these out.
$file = file("index.html", FILE_IGNORE_NEW_LINES);