Program (Source Code) to Trim Whitespaces from Files…
By Angsuman Chakraborty, Gaea News NetworkSunday, 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).
קידום אתרים