PHP实现逐行删除文件右侧空格的方法[原创]_php技巧

WBOY
Release: 2016-05-16 20:01:55
Original
1056 people have browsed it

本文实例讲述了PHP实现逐行删除文件右侧空格的方法。分享给大家供大家参考,具体如下:

在编辑整理代码的过程中发现网上的一些代码经常会有不少的右侧空格,偶尔会影响到代码的排版与阅读,所以写了一段简单的php代码来逐行删除文件右侧的空格,并保存到新的文件中。

带有右侧空格的demo.txt文件(该文件是PHP逐行读取功能代码)如下:

$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); 

Copy after login

PHP逐行删除右侧空格代码如下:

<&#63;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);
&#63;>

Copy after login

运行后即可将删除右侧空格后的文件保存到filetmp.txt

补充:

还可以将带有右侧空格的文件在eclipse环境下编辑保存,不需要代码即可自动删除右侧空格。更加方便。

希望本文所述对大家PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!