Program (Source Code) to Trim Whitespaces from Files…

By Angsuman Chakraborty, Gaea News Network
Sunday, April 20, 2008

PHP is not only a very competent web development language (and part of LAMP stack). It is also a very capable language for writing (command line) scripts. You can write simpler and cleaner scripts with php than perl. Here is a php code which takes a file name as input, trims whitespace from each line of the file and finally saves the result back in the same file.

Save the code to a file named trim.php

< ?php
// In-place trim, use only with array_walk()
function intrim(&$value) {
  $value = trim($value);
}
if($argc > 1) {
  $file = file($argv[1]);
  array_walk($file, 'intrim');
}
// Write to stdout
//echo implode("\n", $file);

// Modify the input file data; dangerous but simple
file_put_contents($argv[1], implode("\n", $file));
?>

Run it as:
php -f trim.php file_to_trim

I use this code to trim whitespace from source code (many editors introduce unnecessary whitespace in files).

Discussion
December 14, 2008: 3:00 am

strongly recommend using joomla. php based cms and very easy to handle!

October 12, 2008: 4:06 am

It only trims very few lines, I think its good for very large pages.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :