This article describes an example of how to delete spaces on the right side of a file line by line in PHP. Share it with everyone for your reference, the details are as follows:
In the process of editing and organizing the code, I found that some codes on the Internet often have a lot of spaces on the right side, which occasionally affects the layout and reading of the code, so I wrote a simple PHP code to delete the right side of the file line by line. spaces and save to a new file.
The demo.txt file with spaces on the right (this file is the PHP line-by-line reading function code) is as follows:
$file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file);
The code for PHP to delete spaces on the right line by line is as follows:
<?php $file=@fopen("demo.txt","r") or exit("file don't exit"); $tmpstr=""; while(!feof($file)){ $tmpstr .= rtrim(fgets($file))."\n"; } fclose($file); file_put_contents("filetmp.txt",$tmpstr); ?>
After running, the file with the spaces on the right deleted can be saved to filetmp.txt
Supplement:
You can also edit and save files with spaces on the right in the eclipse environment, and the spaces on the right can be automatically deleted without coding. More convenient.
I hope this article will be helpful to everyone in PHP programming.