Do you know any good tools for nicely formatting messy php code? Preferably a script for Aptana/Eclipse, but a standalone tool will do too.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Well here is my very basic and rough script:
Here's a php code beautifier (PHP of course) class:
http://www.codeassembly.com/A-php-code-beautifier-that-works/
and
online demo:
http://www.codeassembly.com/examples/beautifier.php
If you use Zend Development Environment, you can use the Indent Code feature (Ctrl+Shift+F).
The simplest solution is to just use an IDE that has this built in. If you're going to be writing code in PHP on a regular a regular basis, just drop the $60 for PHPStorm. You won't regret it.
http://www.jetbrains.com/phpstorm/
It lets you format your code however you like using a simple keyboard shortcut at the file or directory level, and has a zillion other great features.
http://en.sourceforge.jp/projects/pdt-tools/
^^^ will give you a proper CTRL+SHIFT+F Eclipse/Aptana PHP formatter like Java.
See here for installation help.
This is an excellent question. I have an application that reads json and outputs php and html and css. I run a program and generate dozens (hundreds?) of files. I hope the answer here is useful.
I started my project using heredocs, special include files, meta chars, etc but that quickly became a mess. I wanted a stand-alone solution that didn't require framework or ide. So I removed all the heredoc and other junk and created a generic text buffering class with no concern for formatting. It can all be one line for all I care. For html, I do tidy() built-in. For php, I use phpstylist. phpstylist is older but still works well for php format.
To set up the phpstylist options I used UniversalIndent (updated Jan 2012) in windows gui.
UniversalStylist lists 24 (!) formatter programs (c, php, ruby, html,...). It specifically knows the options for phpstylist and gives you a live refresh on a file as you turn options on and off. Very great. Then, when you have your style, it has an option to save the command line options and generates a script. For some formatting options you'll have to add paths to perl, python, etc.
If you are using windows and want to try phpstylist with UniversalIndent, just add directory for php.exe to your env path. I use ampps so mine is set to c:\ampps\php.
It was not very easy to find a good solid solution. I'm also interested in hearing what other people do for simple as possible batch formatting of auto-generated php/html files for code review and archiving purposes.