Pretty-print HTML via PHP without validation?

2020-07-06 04:19发布

I'd like to automatically pretty-print (indentation, mostly) the HTML output that my PHP scripts generate. I've been messing with Tidy, but have found that in its efforts to validate and clean my code, Tidy is changing way too much. I know Tidy's intentions are good but I'm really just looking for an HTML beautifier. Is there a simpler library out there that can run in PHP and just do the pretty-printing? Or, is there a way to configure Tidy to skip all the validation stuff and just beautify?

标签: php html tidy
4条回答
神经病院院长
2楼-- · 2020-07-06 05:01

Facing the same problem i currently use a combination of two commands:

 cat template-home.php | js-beautify --type html | prettier --parser php

js-beautify formats the html bits and prettier formats the php code

查看更多
小情绪 Triste *
3楼-- · 2020-07-06 05:09

I've never used Tidy but it seems pretty customizable.

Here's the quick reference of configuration options: http://tidy.sourceforge.net/docs/quickref.html

But really, with tools like Firebug, I've never seen the need to Tidy HTML output.

查看更多
▲ chillily
4楼-- · 2020-07-06 05:10

The behaviour that you've observed when using Tidy is a result of the underlying use of DOM API. Instead of manipulating the provided source code, DOM API will reconstruct the whole source, thus making fixes along the way.

I've written Dindent, which is a library that uses Regex. It does not do anything beyond adding the indentation and removing whitespaces. However, I advise against using this implementation beyond development purposes.

查看更多
Bombasti
5楼-- · 2020-07-06 05:16

Since you do not want to have it validate for whatever reason, I will not suggest htmlpurifier ; ). Why not just use an IDE to get everything indented nicely, like Alt-Shift-F in Netbeans.

查看更多
登录 后发表回答