Using trim in php

0

I have a file called instructions.txt that is filled with the information that the user writes in a textfield, by clicking on the save button. when creating a new line every time I click through the fwrite code line ($ file, $ content.PHP_EOL); at the end of each line a blank space is created. I have been recommended to use trim but when doing this the lines that I keep are like this:

How can I save line by line without creating a blank space at the end?

    
asked by Estefani_Sanchez 21.05.2017 в 01:13
source

1 answer

1

Try with:

$lineas = explode("\n", $contenido);
$cad = implode("\n", array_map(trim, $lineas));
fwrite($file, $cad);

Or if you want everything in one line

fwrite($file, implode("\n", array_map(trim, explode("\n",$contenido)));

For a long time, all file-writing functions take into account the operating system and change the \ n by the corresponding \ r \ n in Windows or the simple \ n in * nix.

    
answered by 21.05.2017 в 02:11