PHP Code Cleaner and Indenter
With six years of experience with PHP , I have been ‘blessed’ with working with code written by really lazy people , who just didn’t think it was important at all to indent their code or comment it or make the opening and ending curly braces align.
And sometimes our favorite FTP clients would add God knows why those silly annoying empty lines and the code would keep getting bigger and bigger.
And I am sure that I am not alone in my frustrations.
That is why is am writing this script which will try to take care of all that.
Basically it will try to achieve the following goals:
- Indent your code with tabs.
- Put a comment block at the start of class definitions or functions.
- Provide you with the facility to align curly brackets.
- Remove unwanted lines from the code.
- Put some spacing in blocks for improved readability.
- Replaces short open tags with proper <?php tags
- If a PHP block contains only one line , it will put the whole block in one line to improve readability.
Please provide your valuable feedback to improve this script. I will be soon releasing a full script which will clean the code in all PHP files in a directory. Stay tuned!

December 19th, 2007 at 3:27 pm
PHP Code Cleaner and Indenter
[…][…]
December 29th, 2007 at 9:54 am
Great. I’ve looked for something like this befoe but not found a really satisfactory tool. This looks really good on the demo. Might be nice to have the before and after kb data on display.
December 30th, 2007 at 10:12 am
Its cool man,
But is it not that many editors provide a much better indentation.
January 11th, 2008 at 11:23 pm
Brilliant - when will you provide a downloadable version?
February 11th, 2008 at 10:46 am
Sab!
you rocks
March 17th, 2008 at 6:14 pm
Perfect tool! true!
March 24th, 2008 at 5:51 pm
Can’t wait for the batch version download. I need a command prompt driven version.. with a shebang at the top and simple command line parsing.
The second I can do:
# phpcc -R *.php
or similar, I’ll be buying you something on your amazon wishlist.
March 25th, 2008 at 12:05 pm
I find that the following works well using Vim:
Open PHP file in Vim
:source $VIMRUNTIME/indent/php.vim
gg=G
The final command indents the whole file. If you just want to indent a section of the file, visually select the desired section and press ‘=’.
April 8th, 2008 at 9:58 pm
Excellent Tool! Thanks
————————————————–
just to point out an warning on your homepage:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/dphp/public_html/wp-content/plugins/BAStats/BAStats_logger.php on line 114
M
April 25th, 2008 at 10:19 am
Hi, nice script, but.. whats up with replacing [0] with [305] ?
April 26th, 2008 at 2:45 pm
Useful tool with a nice and clean interface.
Hope to get the same result with code ^^
and give it 5 stars
Cheers
April 29th, 2008 at 2:33 am
Cheers for the fix Sab, now if only i could plug it into eclipse
hint hint nudge nudge
Happy Hacking
May 9th, 2008 at 1:52 am
hi,
great tool.
minor error
$test= (isset($_POST[’test’]) ? clean($_POST[’test’]) : ”);
becomes
$test= (isset($_POST[’test’]) ? clean($_POST[’test’]) :
”);