This article describes how to delete spaces on the right of a file row by row in PHP, including opening a file in php, reading files row by row, deleting spaces on the right of the rtrim function, and saving files, for more information about how to delete spaces on the right of a file row by row in PHP, see the example in this article. We will share this with you for your reference. The details are as follows:
In the process of editing and organizing the code, it is found that some code on the Internet often has a lot of spaces on the right side, which occasionally affects the code layout and reading, therefore, I wrote a simple php code to delete the space on the right of the file line by line and save it to the new file.
The demo.txt file with the right-side space (this file is the PHP row-by-row read 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 )."
";} Fclose ($ file );
The code for deleting spaces on the right side of PHP 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, you can save the files deleted from the right-side space to filetmp.txt.
Supplement:
You can edit and save files with spaces on the right in the eclipse environment, and automatically delete spaces on the right without code. More convenient.
I hope this article will help you with PHP programming.