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!


PHP Code Cleaner and Indenter
[...][...]
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.
Its cool man,
But is it not that many editors provide a much better indentation.
Brilliant – when will you provide a downloadable version?
Sab!
you rocks
Perfect tool! true!
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.
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 ‘=’.
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
Hi, nice script, but.. whats up with replacing [0] with [305] ?
Useful tool with a nice and clean interface.
Hope to get the same result with code ^^
and give it 5 stars
Cheers
Cheers for the fix Sab, now if only i could plug it into eclipse
hint hint nudge nudge
Happy Hacking
hi,
great tool.
minor error
$test= (isset($_POST['test']) ? clean($_POST['test']) : ”);
becomes
$test= (isset($_POST['test']) ? clean($_POST['test']) :
”);
You going to release the code anytime soon?
btw, it breaks on Public functions in class.
It does
public
//comment
//Author
//By
function myfunction() …
[...] http://www.decodephp.com/2007/12/19/php-code-cleaner-and-indenter/ [...]
Worked for me. Great tool, thanks from Argentina !
Bug : plz, make a space before else
Can your code handle utf8 lettering like hebrew, arabic, etc.
Man, you made my year with this one. So much pain gone now…. gone now…
Nice tool, seems a bit buggy with switch case statements though. Good job.