使用换行符和制表符自动格式化 HTML/PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6516318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:37:02  来源:igfitidea点击:

Auto Format HTML/PHP With Line Breaks and Tabs

phphtml

提问by mellowsoon

I've run into a problem. Someone on our team edited a HTML/PHP file, and the entire contents of the file ended up on a single line. All line breaks and tabs were completely removed from the file. Its a very large file, so trying to edit it manually would take forever.

我遇到了一个问题。我们团队中的某个人编辑了一个 HTML/PHP 文件,该文件的全部内容最终出现在一行中。所有换行符和制表符都从文件中完全删除。它是一个非常大的文件,因此尝试手动编辑它会花费很长时间。

Does anyone know of a tool I can use to re-format the code?

有谁知道我可以用来重新格式化代码的工具?

回答by Knowledge Craving

Use the NetBeans IDE, for the development purpose. Then open the file in that IDE and press these keys at the same time - Alt+ Shift+ F.

使用NetBeans IDE进行开发。然后在该 IDE 中打开文件并同时按下这些键 - Alt+ Shift+ F

The file will get formatted nicely, and also will show you if there are any errors in that file.

该文件将被很好地格式化,并且还会显示该文件中是否有任何错误。

回答by alex

You could use DOMDocumentto tidy the HTML.

您可以DOMDocument用来整理 HTML。

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadHTML($html);
$dom->formatOutput = TRUE;

echo $dom->saveHTML();

回答by Amber

(First link removed per comment below.)

(下面每个评论删除了第一个链接。)

http://www.phpformatter.com/is an option.

http://www.phpformatter.com/是一个选项。

回答by Ravan Scafi

You can use tidy(html only) or codecleaner

您可以使用tidy(仅 html)或codecleaner

回答by Bùi Xuan H?i

In Aptana or Zend Studio or Eclipse PHP, you can press CTRL+ SHIFT+ Fto auto format your code. Remember to fix all syntax errors before.

在 Aptana 或 Zend Studio 或 Eclipse PHP 中,您可以按CTRL+ SHIFT+F自动格式化您的代码。请记住之前修复所有语法错误。

回答by Clay Fowler

Visual Studio. Open the HTML file then press ctrl k then ctrl d (or use the edit menu).

视觉工作室。打开 HTML 文件,然后按 ctrl k 和 ctrl d(或使用编辑菜单)。