Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

PHP: Strip empty lines and comments from files in a directory recursively

In a recent project I needed to strip all the empty lines and comments from all the files in a directory and it’s sub directories. Being a PHP developer I naturally turned to PHP to do the work for me. What I came up with is a simple solution that let’s you send in a directory path as a parameter. That directory is then recursively searched (goes into all the directories). All the files found are opened, empty lines and comments are stripped, then the file is overwritten with the clean file. This is the only line of code you need:


This works great for my purposes. If you are wanting to do the same you will need to download the attached PHP file as it contains the implementation of the functions needed to pull this off.

Download the PHP file with the required functions here

Take note that this opens EVERY file. In the future I might expand it to specific file extensions.

Questions, Comments? Leave them below


Movie: Legend of the Guardians: The Owls of Ga’hoole


My new desk mate Peggy


  1. Hi,

    thank you! Helped me a lot. 🙂

    Found one bug:
    Double slash inside of a string is recognized as comment. That’s wrong. :-/
    Sample: echo ‘This is a string with an URL‘;


  2. kevin

    love you dude… you are awesome 🙂

Leave a Reply

Powered by WordPress & Drip City Coffee