浏览代码

simplify trailing space removal

Tim Fry 4 月之前
父节点
当前提交
325399e724
共有 1 个文件被更改,包括 4 次插入15 次删除
  1. 4 15
      file_save.php

+ 4 - 15
file_save.php

@@ -148,26 +148,15 @@
 						//build a array of the content
 						$lines = explode("\n", str_replace ("\r\n", "\n", $_POST["content"]));
 
-						//go in reverse order to remove blank trailing lines at the end of the file
-						for ($i = count($lines) - 1; $i > 0; $i--) {
-							//find the last empty line
-							if (!empty($lines[$i])) {
-								//exit for loop
-								break;
-							}
-							//remember the last line index
-							$last_line = $i;
-						}
-
 						$file_content = '';
 
 						//remove trailing spaces on each line
-						for ($i = 0; $i < $last_line; $i++) {
-							$file_content .= rtrim($lines[$i]) . "\n";
+						foreach ($lines as $line) {
+							$file_content .= rtrim($line) . "\n";
 						}
 
-						//save the file
-						fwrite($handle, $file_content);
+						//save the file with single empty line
+						fwrite($handle, rtrim($file_content) . "\n");
 
 						//close the file handle
 						fclose($handle);