Is there any PHP Tidy alternative to only tab-indent HTML output? I need the latter for development/debug purposes only to go through the generated output code. Though, as much as I tried to configure Tidy for this simple task, I couldn't without preventing other changes.
-
I think it is possible to complete your question with the Tidy configurations and/or a example of software (blackbox) that do the same work. This kind of software are the "codeType-beautifier" (JS-beautifier for javascript, HTML-beautifier for HTML, etc.). Google example: http://www.digitalcoding.com/tools/html-beautifier.html – Peter Krauss Aug 21 '12 at 06:11
-
PHP Tidy is a good option for the task, because recognize any pattern of (X)HTML and is faster than a PHP program... Your problem generates another parallel question (perhaps without bounty): "Who to configure PHP Tidy only for tab-indent HTML without side effects?" – Peter Krauss Aug 21 '12 at 06:27
-
What configurations have you tried? Can you give examples of your input and your expected output too please? – Homer6 Aug 24 '12 at 19:58
5 Answers
Two years later and there is still no library to achieve HTML output indentation without using implementations that rely on DOM API (ie. Tidy and alike).
I've developed library that tokenises HTML input using regular expression. None of the HTML is changed beyond adding the required spacing for indentation.

- 69,002
- 70
- 275
- 438
I always use jsbeautifier. Though it doesn't follow my standards with javascript, the html indentation is awesome.
EDIT: Before you downvote, notice that jsbeautifier is open source, and has ports in several languages, all serverside: https://github.com/einars/js-beautify

- 4,531
- 4
- 19
- 49
-
-
-
@Guy, this might be of your interest https://github.com/einars/js-beautify/blob/master/php/test.php#L6 in jsbeautifier, +1 for that btw – Adi Jul 13 '12 at 04:33
-
The only thing I don't get is – how is this supposed to work for HTML? https://github.com/einars/js-beautify/blob/master/php/test.php doesn't give a single example nor it is clear from looking at the class itself. – Gajus Jul 13 '12 at 04:45
-
Ops, "JS beautifier", as the name say, is only for JS. Is not for HTML (see question text). $result = $jsb->beautify('your javascript string'); – Peter Krauss Aug 21 '12 at 06:07
-
At the time, I wasn't aware that the html beautifying aspect of the library was had not been ported :S – Korvin Szanto Sep 11 '12 at 22:56
You can try the htmLawed library. It's a Tidy alternative for PHP. If you just need an indenting function, you can use the code for the hl_tidy function of the library.
// indent using one tab per indent, with all HTML being within an imaginary div
$out = hl_tidy($in, 't', 'div')

- 327
- 4
- 13
I use LogicHammers HTMLFormatter which you need to pay for but is worth every penny. Use it to format the html before you look at it and it makes it much easier.

- 10,997
- 11
- 73
- 124
Though this is not the exact answer , see if this helps you. I use netbeans and to make code indented I simply right click and Format the code. If you are using any other IDE search for similar functionality or may be you can import with help of 3rd party plugins.

- 4,220
- 5
- 26
- 37