PHP实现逐行删除文件右侧空格的方法 <font color=red>原创</font>
author:一佰互联 2019-04-27   click:139

本文实例讲述了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)).""; } fclose($file); file_put_contents("filetmp.txt",$tmpstr);?>

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

补充:

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

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