Contact: Web / Voice / Email / Tips
Simple Thoughts Blog - Java and Web Technologies

Simple solutions for complex problems.

 

Program (Source Code) to Trim Whitespaces from Files…

April 20th, 2008 by Angsuman Chakraborty

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).


Filed under Headline News, How To, PHP, Programming, Tech Note, Web | | RSS 2.0 | Trackback this Article | Email this Article

You may also like to read

»How To Add Trim Functionality To Javascript String
»How To trim() in Javascript
»Subversion: How To Ignore Files From Status Display
»5 FaceBook Source Code Secrets
»Microsoft Sues FairUse4WM, Windows Media DRM Remover, Developer
»How To Clean Subversion Directories
»How To: PHP Script To Windows EXE File Generation
»Simplifying Java Software Development: How to Count the Number of Words in a String using Java (one-liner)
»How To Recursively Delete Files And Directories Using Java
»BandSite CMS and SmartSite CMS (PHP based) Root File Inclusion Vulnerability Discovered
»How To Backup PmWiki in Subversion (Code)
»GMail Email Attachment Policy
»Dr. Richard Stallman is talking nonsense; A look into Java Trap
»How To Delete Trash From Command Line in Linux / Fedora Core 6
»5 Minutes Guide To Subversion

Looking forward to hear your thoughts.



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment.

No. 1 method to ethically increase your blog traffic and reach.

Translate

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanPřeložit do Čech/CzechOversætte hen til Dansk/DanishKääntää jotta Finnish/FinnishLefordít -hoz Magyar/HungarianÞýða til Íslenska/IcelandicTraducir a Latinoamericano Español/Latin American Spanishtagapagsalin sa Filipino/FilipinoTłumaczyć wobec Polski/PolishA traduce la spre Român/RomanianPrevesti za Srpski/Serbiantolmačiti v slovenski/SlovenianÖversätta till Svensk/SwedishChyfieitha at Cymraeg/Welshtercüme etmek -e doğru Türk/TurkishPrevesti to Hrvatski/CroatianПревеждам към Българин/BulgarianTraduzca al Español/SpanishTraduisez au Français/FrenchTraduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplifiedترجمة الى العربية/ArabicVertaal aan het Nederlands/DutchΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/Norwegian中文翻译/Chinese TraditionalTraduzir a Língua portuguesa brasileira/Brazilian PortugueseReddo ut Latin/Latin

Taragana Network

»Ctrl-S
»Enterprise Blog
»Free Book on Eye Care by Natural Therapy
»Health Care Blog
»Hot Computer Jobs Blog
»Pet Care & Grooming News and Tips
»Phil Law Blog
»Taragana - Software Outsourcing
»The Angsuman Chakraborty Blog
»The Diabetes Cure Blog
»The Eye Treatment Blog
»The Stem Cell Blog
»Weblog Hosting Blog
"I am convinced that He (God) does not play dice." - Albert Einstein