本文实例讲述了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); php逐行删除右侧空格代码如下:
<?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);
?>运行后即可将删除右侧空格后的文件保存到filetmp.txt
补充:
还可以将带有右侧空格的文件在eclipse环境下编辑保存,不需要代码即可自动删除右侧空格。更加方便。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!