摘要:本文介绍了一个 PHP 类 CodeFormat 的实现,用于对 HTML 代码进行压缩和格式化。该类提供 miniHtmlCode 方法压缩代码(去除注释、合并空白),以及 fomatHtmlCode 方法格式化代码(对 HTML、CSS、JS 分别进行缩进和换行处理),并包含去除各类注释的辅助方法。
-
CodeFormat.php
<?php
namespace app\core;
class CodeFormat
{
//压缩code
public static function miniHtmlCode($code)
{
$code = trim($code);
$code = self::removeComment($code, 'html');
$code = preg_replace('/\s+/', ' ', $code);
$code = preg_replace('/\>\s+\</', '><', $code);
return $code;
}
//格式化code
public static function fomatHtmlCode($code)
{
$code = self::miniHtmlCode($code);
preg_match('/' . preg_quote('<style>', '/') . '(.*?).' . preg_quote('</style>', '/') . '/', $code, $style);
if (!empty($style)) {
$style = $style[0];
$style = str_replace(['<style>', '</style>'], '', $style);
$style = self::fomatCssCode($style);
$code = preg_replace('/' . preg_quote('<style>', '/') . '(.*?).' . preg_quote('</style>', '/') . '/', '@style', $code);
}
preg_match('/' . preg_quote('<script>', '/') . '(.*?).' . preg_quote('</script>', '/') . '/', $code, $script);
if (!empty($script)) {
$script = $script[0];
$script = str_replace(['<script>', '</script>'], '', $script);
$script = self::fomatScriptCode($script);
$code = preg_replace('/' . preg_quote('<script>', '/') . '(.*?).' . preg_quote('</script>', '/') . '/', '@script', $code);
}
$p = preg_replace('/^\</', '', $code);
$p = preg_replace('/\>$/', '', $p);
$p = explode('><', $p);
$p = preg_replace('/^/', '<', $p);
$p = preg_replace('/$/', '>', $p);
$n = 0;
$labels = [];
foreach ($p as $key => $item) {
$res = '';
preg_match('/^(\<\/?)(.*?)(\s|\>)/', $item, $res);
$label = $res[0];
$label = str_replace(" ", '', $label);
if ($label[strlen($label) - 1] !== '>') {
$label .= '>';
}
$labels[] = $label;
}
$labels = array_unique($labels);
sort($labels);
$labels_l0 = [
'<html>',
'</html>',
'<body>',
'</body>',
];
foreach ($p as $key => $item) {
$res = '';
preg_match('/^(\<\/?)(.*?)(\s|\>)/', $item, $res);
$label = $res[0];
$label = str_replace(" ", '', $label);
if ($label[strlen($label) - 1] !== '>') {
$label .= '>';
}
if ($label[1] !== '/') {
$p[$key] = self::addPadding($n) . $item . "\n";
$trmp = str_replace('<', '</', $label);
if (in_array($trmp, $labels)) {
if (preg_match('/' . preg_quote($trmp, '/') . '$/', $item)) {
} else {
//双标签
$n += 4;
if (in_array($label, $labels_l0)) {
$n = 0;
}
}
} else {
//单标签
}
} else {
$n -= 4;
if (in_array($label, $labels_l0)) {
$n = 0;
}
$p[$key] = self::addPadding($n) . $item . "\n";
}
}
$s = implode($p);
if (!empty($style)) {
$s = preg_replace('/@style/', $style, $s);
}
if (!empty($script)) {
$s = preg_replace('/@script/', $script, $s);
}
return $s;
}
//格式化js code
public static function fomatScriptCode($script)
{
$script = self::removeCommentJS($script);
// $script = preg_replace('/\s+/',' ',$script);
$script = preg_replace('/;/', ";\n", $script);
$script = preg_replace('/\s+\{/', "{\n", $script);
$script = preg_replace('/\s+\}/', "\n}\n", $script);
$p = explode("\n", $script);
$s = '';
$n = 0;
// $p = array_filter($p);
foreach ($p as $key => $item) {
if (empty($item)) {
$p[$key] = "\n";
continue;
}
if ($n < 0)
$n = 0;
if (preg_match('/^function/', $item)) {
$n = 0;
}
if ($item[strlen($item) - 1] === '{') {
$p[$key] = self::addPadding($n) . $item . "\n";
$n += 4;
} else if ($item[strlen($item) - 1] === '}') {
$n -= 4;
$p[$key] = self::addPadding($n) . $item . "\n";
} else {
$p[$key] = self::addPadding($n) . $item . "\n";
}
}
$s = implode($p);
$s = "\n" . '<script>' . "\n" . $s . '</script>' . "\n";
return $s;
}
//格式化css code
public static function fomatCssCode($style)
{
$style = self::removeCommentCSS($style);
$style = preg_replace('/\{/', "{\n", $style);
$style = preg_replace('/;/', ";\n", $style);
$style = preg_replace('/\}/', "}\n\n", $style);
$p = explode("\n", $style);
$s = '';
$n = 8;
// $p = array_filter($p);
foreach ($p as $key => $item) {
if (empty($item)) {
$p[$key] = "\n";
continue;
}
if ($n < 8)
$n = 8;
if ($item[strlen($item) - 1] === '{') {
$p[$key] = self::addPadding($n) . $item . "\n";
$n += 4;
} else if ($item[strlen($item) - 1] === '}') {
$n -= 4;
$p[$key] = self::addPadding($n) . $item . "\n";
} else {
$p[$key] = self::addPadding($n) . $item . "\n";
}
}
$s = implode($p);
$s = "\n" . '<style>' . "\n" . $s . '</style>' . "\n";
return $s;
}
//添加占位符
private static function addPadding($n)
{
$p = '';
while ($n > 0) {
$n--;
$p .= ' ';
}
return $p;
}
//去除注释
public static function removeComment($code, $language = 'all')
{
$languages = [
'html', 'css', 'js'
];
if ($language === 'all') {
foreach ($languages as $language) {
$code = self::removeComment($code, $language);
}
return $code;
} else if (in_array($language, $languages)) {
switch ($language) {
case 'php':
return self::removeCommentPHP($code);
break;
case 'html':
return self::removeCommentHTML($code);
break;
case 'css':
return self::removeCommentCSS($code);
break;
case 'js':
return self::removeCommentJS($code);
break;
}
} else {
return $code;
}
}
//去除php注释
private static function removeCommentPHP($code)
{
$code = preg_replace('/(\/\/(.*?)\n)|(^\#(.*?)\n)|(\/\*(.*?)\*\/)/sm', '', $code);
return $code;
}
//去除js注释
private static function removeCommentJS($code)
{
$code = preg_replace('/(\/\/(.*?)\n)|(\/\*(.*?)\*\/)/sm', '', $code);
return $code;
}
//去除css注释
private static function removeCommentCSS($code)
{
$code = preg_replace('/(\/\*(.*?)\*\/)/sm', '', $code);
return $code;
}
//去除html注释
private static function removeCommentHTML($code)
{
$code = preg_replace('/(\<\!\-\-(.*?)\-\-\>)/sm', '', $code);
return $code;
}
}
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/etafort/article/details/165892522



