Any way to mass auto indent php and html code in Ubuntu?
Solution 1:
I regularly use a tool called HTML Tidy to clean up dirty HTML. The Ubuntu package is simply called "tidy".
For PHP there seems to exist a tool called phptidy, but apart from testing if it works, I never used it.
Solution 2:
If you have worked with Python before and the code is HTML or XML, then I would suggest using BeautifulSoup and automate the process for all files. In a Python console run this:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print(soup.prettify())
Here html_doc
is just the text of the file to be indented.
Or the non-geeky was is to use some IDE to get it done individually.
Solution 3:
PHPFormatter is a php script found here -- https://github.com/NathanWong/phpformatter It can be run via the command line and does an awesome job of formatting PHP,HTML,and Javascript. From the authors description:
"This PHP script will format another PHP script using my arbitrarily set formatting rules (real tabs, 97 chars per line, brackets on their own line, indentation based on operator hierarchy not alignment, spaces around most operators but not commas or brackets, space after if, etc.). It will also format inline HTML, and if JS Beautify is available, it'll use that for inline Javascript in inline HTML too."
It works great in Geany (sudo apt-get install geany). I simply unzipped the 2 files : format.php and jsbeautify.php to my home directory and then added the following custom command in Geany (Edit->Format-Send Selection To->Set Custom Commands):
php /home/myusername/format.php -
(Note the dash at the end. This tells the script to accept input from the standard input stream which is where Geany sends the text.) Now all you have to do is a quick Ctrl+a (SelectAll) then Ctrl+1 (or whichever number you put the command on) and ...
Voila...
Beautifully formatted code!! Thanks Nathan!
Solution 4:
maybe you could try phptidy command line tool which works on Linux and Windows