Php code formatter / beautifier and php beautification in general
PHP Code Beautifier is a useful free tool that should do what you're after, although their download page does require an account to be created.
The tool has been declined into 3 versions:
- A GUI version which allow to process file visually.
- A command line version which allow to be batched or integrated with other tools (CVS, SubVersion, IDE ...).
- As an integrated tool of PHPEdit.
Basically, it'll turn:
if($code == BAD){$action = REWRITE;}else{$action = KEEP;}
for($i=0; $i<10;$i++){while($j>0){$j++;doCall($i+$j);if($k){$k/=10;}}}
into
if ($code == BAD) {
$action = REWRITE;
} else {
$action = KEEP;
}
for($i = 0; $i < 10;$i++) {
while ($j > 0) {
$j++;
doCall($i + $j);
if ($k) {
$k /= 10;
}
}
}
Need magic php code-formatter
Netbeans is a free IDE that has the ability to format your code.
Format code command for PHP/HTML in Visual Studio Code
Update 2021-07-21
It's been more than half a decade since I first wrote this answer. The extensions to which I originally linked are abandoned, and Visual Studio Code's intrinsic PHP support hasn't improved, which is disappointing. The only decent extension still standing of which I'm aware is PHP Intelephense, which uses a freemium model: basic features are free, and a lifetime license is $12 USD as of writing.
The free version of Intelephense supports code formatting with the usual shortcuts (Alt + Shift + F on Windows and Linux, ⌥⇧F on macOS). Visual Studio Code continues to lack built-in support for PHP code formatting and will direct you to the extension marketplace if you attempt to format PHP without an appropriate extension installed.
Original answer
Visual Studio Code has pretty awesome PHP support. What it lacks is covered by extensions. A quick search reveals at least three (1, 2, and 3) that claim to support PHP formatting.
They mostly seem to use the standard shortcut of Alt + Shift + F on Windows/Linux, with varying shortcuts on Mac. If you're on Mac, give ⌥⇧F a try.
Dreamweaver extension to beautify PHP/JavaScript/jQuery code
Online solutions for validation and formatting JavaScript:
Validate JavaScript code:
http://www.javascriptlint.com/
Format JavaScript code:
http://jsbeautifier.org/
Beautify HTML stored in a string on PHP
Using DomDocument
we load the html passing the LIBXML_HTML_NOIMPLIED
flag
which will prevent the loadHTML
method to add the extra html
wrapper.
We save as XML to get the nice indentation, while passing the $dom->documentElement
parameter to prevent the XML
header.
$html = '<body><div><p>hello</p><div></body>';
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($html,LIBXML_HTML_NOIMPLIED);
$dom->formatOutput = true;
print $dom->saveXML($dom->documentElement);
This will output
<body>
<div>
<p>hello</p>
<div/>
</div>
</body>
Notice that the HTML
was fixed for you as the second div
should have been a closing tag, I assume.
If we pass the proper HTML
as the input string, the output will be as you require
$html = '<body><div><p>hello</p></div></body>';
<body>
<div>
<p>hello</p>
</div>
</body>
PHP HTML Formatter Beautifier
Eclipse with setted up by you formatting rules. Both HTML and PHP formatter. And it's free.
VSCode Prettier not formatting PHP
Core prettier does not support PHP1, and thus neither does the plugin for VSCode/VSCodium.
Luckily the designers of prettier seems to have thought of this and implemented a plugin system2.
According to the prettier-vscode repo, all you need to do to use a plugin is to add it and prettier to your package.json
3
So for php support your package.json would need to contain:
{
"devDependencies": {
"@prettier/plugin-php": "0.14.3",
"prettier": "2.0.5"
}
}
How can I format PHP files with HTML markup in Visual Studio Code?
The extension Beautify (from HookyQR) just does it very well. Either add PHP, and any other file extension type, to the configuration. As said by Nico, here is an example:
Go to user settings (Ctrl + Shift + P → User settings (UI) or Ctrl + , (comma)
Search for Beautify in the field above. And click on "Edit in settings.json" for "Beautify: Config".
For the "html" section, add "php" and "blade".
###Usage
You can invoke it directly. Press F1, and then write Beautify. The auto completion gives you two choices, "Beautify file" and "Beautify selection". Choose the one you need, and it will do the job. That's a straightforward direct way.
You can also add a keybinding to have a keyboard shortcut. Here is how to do it:
Open keybindings.json (go to menu File → Preferences → Keyboard Shortcuts)
Click in the above. Open and edit file keybindings.json
Add the following into the closed brackets, []
{
"key": "alt+b",
"command": "HookyQR.beautify",
"when": "editorFocus"
}Choose any key you want, and make sure you don't override an existing one. Search first in the left side if it exists or not.
Note that all of those things are well documented in the description of the extension.
Extra: Note about blade
(suite to @Peter Mortensen clarification pinpoint)
blade or blade.php
If you are confused if it's blade
or blade.php
for the setting! I'll clear it up for you! It's blade
! That's vscode keyword for the language!
How do you know ?
First if you open the list of languages as by the image bellow:
Write blade
You can see Laravel Blade (blade)
! The language keyword is in the paratheses! blade!
Well but how to check!
Try with blade.php
in the settings!
Try to beautify
You'll get an asking context menu for what language (html, css, js)!
So it doesn't works!
To really know ! Put back blade
!
And it will work and beautify directly!
How well it works with blade
The awesome answer to that! Is try it, and see for yourself!
But if you ask me! I'll say it does work as with html! It can be too handy! And you may need to fix some lines! Depending on your expectations and your style!
Here an illustration! I screwed the indentation in purpose
And here the beautification result:
Is it possible to auto format PHP in Sublime Text 2?
I've been searching this before also, but with no avail. Sublime text 2 probably doesn't support formatting PHP code natively. I have looked into ST2 packages - http://wbond.net/sublime_packages/community, but there is also nothing. Probably you'll have to do this in another editor.
Of course it would be nice, if someone would write a package for this, 'cause packages for formatting JS, JSON, HTML.. already exist.
Related Topics
PHP Not Loading PHP_Pgsql.Dll on Windows
Handle Guzzle Exception and Get Http Body
Installing Pdo Driver on MySQL Linux Server
Running PHP Script from the Command Line
"Distance" Between Colours in PHP
JSON VS. Serialized Array in Database
Laravel:Migrations & Seeding for Production Data
How to Learn About PHP Internals
How Does "Do Something or Die()" Work in PHP
Best Place for Validation in Model/View/Controller Model
What's the Best Way to Create a Single-File Upload Form Using PHP
Youtube I.D Parsing for New Url Formats