php正则表达式压缩与格式化html,css,js

注意事项

只支持压缩含一个<script></script>的html,且变量内多个空格也会被压缩为一个

格式化html单标签需要添加/结束

压缩html

去除<!-- -->内的全部内容

多个空白符变为一个空格

去除> <内的空白符

压缩css

去除/* */内的全部内容

多个空白符变为一个空格

去除【'】【"】【{】【}】【:】【;】【,】【<style>】【</style>】两端的空白符

压缩js

去除/* */内的全部内容
去除//行内容
多个空白符变为一个空格
去除【(】【)】【{】【}】【=】【||】【&&】【+】【:】【;】【,】【<script>】【</script>】两端的空白符

调用 

<?php

include 'HtmlFormat.php';

$html = file_get_contents('https://www.baidu.com/');

//压缩

$min_html = HtmlFormat::miniHtml($html);

highlight_string($min_html);

//格式化

$max_html = HtmlFormat::fomatHtml($html );

highlight_string($max_html);

代码

压缩与格式化

HtmlFormat.php

<?php
namespace ppt\core;
class HtmlFormat
{public static function miniHtml($s){//注:只支持压缩含一个<script></script>的html,且变量内多个空格也会被压缩为一个//提取css跟javascript单独处理$s = preg_replace('/\<style[\s\S]*\>/U','<style>',$s);
//    preg_replace('/\<script[\s\S]*\>/','<script>',$s);preg_match('/\<style\>[\s\S]*\<\/style\>/',$s,$style);preg_match('/\<script\>[\s\S]*\<\/script\>/',$s,$script);empty($style)?$style='':$style = $style[0];empty($script)?$script='':$script = $script[0];//处理html$html = preg_replace('/\<style\>[\s\S]*\<\/style\>/','<style></style>',$s);$html = preg_replace('/\<script\>[\s\S]*\<\/script\>/','<script></script>',$html);$html = preg_replace('/\<\!\-\-[\s\S]*\-\-\>/U','',$html);$html = preg_replace('/[\s]+/',' ',$html);$html = preg_replace('/>[\s]+</','><',$html);//处理css$style = preg_replace('/\/\*[\s\S]*\*\//U','',$style);$style = preg_replace('/[\s]+/',' ',$style);$style = preg_replace('/[\s]?\'[\s]?/','\'',$style);$style = preg_replace('/[\s]?"[\s]?/','"',$style);$style = preg_replace('/[\s]?\{[\s]?/','{',$style);$style = preg_replace('/[\s]?\}[\s]?/','}',$style);$style = preg_replace('/[\s]?\:[\s]?/',':',$style);$style = preg_replace('/[\s]?;[\s]?/',';',$style);$style = preg_replace('/[\s]?,[\s]?/',',',$style);$style = preg_replace('/[\s]?\<style\>[\s]?/','<style>',$style);$style = preg_replace('/[\s]?\<\/style\>[\s]?/','</style>',$style);//处理js$script = preg_replace('/\/\*[\s\S]*\*\//U','',$script);$script = preg_replace('/^[\s]*\/\/.*$\n/m','',$script);$script = preg_replace('/[\s]+/',' ',$script);$script = preg_replace('/[\s]?\([\s]?/','(',$script);$script = preg_replace('/[\s]?\)[\s]?/',')',$script);$script = preg_replace('/[\s]?\{[\s]?/','{',$script);$script = preg_replace('/[\s]?\}[\s]?/','}',$script);$script = preg_replace('/[\s]?\=[\s]?/','=',$script);$script = preg_replace('/[\s]?\|\|[\s]?/','||',$script);$script = preg_replace('/[\s]?\&\&[\s]?/','&&',$script);$script = preg_replace('/[\s]?\+[\s]?/','+',$script);$script = preg_replace('/[\s]?\:[\s]?/',':',$script);$script = preg_replace('/[\s]?;[\s]?/',';',$script);$script = preg_replace('/[\s]?,[\s]?/',',',$script);$script = preg_replace('/[\s]?\<script\>[\s]?/','<script>',$script);$script = preg_replace('/[\s]?\<\/script\>[\s]?/','</script>',$script);//合并css跟js$html = preg_replace('/\<style\>[\s\S]*\<\/style\>/',$style,$html);$html = preg_replace('/\<script\>[\s\S]*\<\/script\>/',$script,$html);return $html;}//格式化htmlpublic static function fomatHtml($content){$content = self::miniHtml($content);preg_match('/\<style\>[\s\S]*\<\/style\>/',$content,$style);preg_match('/\<script\>[\s\S]*\<\/script\>/',$content,$script);empty($style)?$style='':$style = $style[0];empty($script)?$script='':$script = $script[0];$style = str_replace('<style>',"<style>\n",$style);$style = preg_replace('/;\}\}/',"@#",$style);$style = preg_replace('/;\}/',"@@",$style);$style = preg_replace('/\}/',"$$$",$style);$style = preg_replace('/;/',";\n\t",$style);$style = preg_replace('/@#/',";\n\t}\n}\n\n",$style);$style = preg_replace('/@@/',";\n}\n",$style);$style = preg_replace('/\$\$\$/',"\n}\n\n",$style);$style = preg_replace('/\{/',"{\n\t",$style);$style = preg_replace('/\n/',"\n\t\t",$style);$style = str_replace("\t</style>","</style>",$style);$script = self::format_javascript($script);$content = preg_replace('/\<style\>[\s\S]*\<\/style\>/','<style></style>',$content);$content = preg_replace('/\<script\>[\s\S]*\<\/script\>/','<script></script>',$content);$tmp = explode('><', $content);$new = [];foreach ($tmp as $key => $item) {if ($key === 0) {$item .= '>';$new[] = $item;} else if ($key === count($tmp) - 1) {$item = '<' . $item;$new[] = $item;} else {if (strpos($item, '<') !== false || strpos($item, '>') !== false) {$tmp1 = str_replace(['<', '>'], '@', $item);$tmp2 = explode('@', $tmp1);$new[] = '<' . $tmp2[0] . '>';$new[] = str_replace(" ",'',$tmp2[1]);$new[] = '<' . $tmp2[2] . '>';} else {$item = '<' . $item . '>';$new[] = $item;}}}$i = 0;$str = '';foreach ($new as $key => $item) {if(substr($item,0,strlen('<html'))==='<html'||substr($item,0,strlen('<body'))==='<body'){$i=0;}if($key>0){if(substr($new[$key-1],0,strlen('<body'))==='<body')$i=0;}if(strpos($item,'<')===false&&strpos($item,'>')===false){//内容$str.= $item;}else if(substr($item,strlen($item)-2,2)=='/>'){//单标签$p = '';$k = $i;while ($k>0){$p.="\t";$k--;}$str.= $p;$str.= $item;$str.= "\r\n";}else if(strpos($item,'<meta')!==false||strpos($item,'<link')!==false||strpos($item,'<input')!==false||strpos($item,'<img')!==false||strpos($item,'<hr')!==false||strpos($item,'<br')!==false||strpos($item,'<html')!==false){//单标签$p = '';$k = $i;while ($k>0){$p.="\t";$k--;}$str.= $p;$str.= $item;$str.= "\r\n";}else{if(substr($item,0,2)!=='</'){//双标签开始$p = '';$k = $i;while ($k>0){$p.="\t";$k--;}if($key!==(count($new)-2)){if(strpos($new[$key+1],'<')===false&&strpos($new[$key+1],'>')===false){$str.= $p;$str.= $item;}else{$str.= $p;$str.= $item;$str.= "\r\n";}}else{$str.= $p;$str.= $item;$str.= "\r\n";}$i++;}else{//双标签结束$i--;$p = '';$k = $i;while ($k>0){$p.="\t";$k--;}if($key>0){if(strpos($new[$key-1],'<')===false&&strpos($new[$key-1],'>')===false){}else{$str.= $p;}}else{$str.= $p;}$str.= $item;$str.= "\r\n";}}}$str = preg_replace('/\<style\>[\s\S]*\<\/style\>/',$style,$str);$str = preg_replace('/\<script\>[\s\S]*\<\/script\>/',$script,$str);$str = preg_replace('/\n[\s]*\n/m',"\n",$str);return $str;}//格式化jsprivate static function format_javascript($js_code) {$js_code = str_replace(['<script>','</script>'],'',$js_code);$js_code = str_replace(';',";\n",$js_code);$js_code = str_replace('{',"\n{\n",$js_code);$js_code = str_replace('}',"\n}\n",$js_code);$tmp = explode("\n",$js_code);$tmp = array_filter($tmp);$lines = '';$index = 0;$space = '';foreach ($tmp as $item){if(strpos($item,'}')!==false){$index--;}$space = '';for ($i=0;$i<$index;$i++){$space.="\t";}$lines.="\n".$space.$item;if(strpos($item,'{')!==false){$index++;}}$lines = str_replace("\nfunction","\n\nfunction",$lines);$lines = str_replace("\n;",";",$lines);$lines = preg_replace('/\}[\s]+\)/',"})",$lines);$lines = preg_replace('/\)[\s]+\{/',"){",$lines);$lines="<script>\n".$lines."\n</script>";return $lines;}
}
//压缩html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/824768.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汽车零部件制造迎来智能化升级,3D视觉定位系统助力无人化生产线建设

随着新能源汽车市场的蓬勃发展&#xff0c;汽车零部件制造行业正面临着前所未有的机遇与挑战。为了提高产能和产品加工精度&#xff0c;某专业铝合金汽车零部件制造商决定引进智能生产线&#xff0c;其中&#xff0c;对成垛摆放的变速箱壳体进行机床上料成为关键一环。 传统的上…

SpringBootSpringCloud升级可能会出现的问题

1.背景 之前负责过我们中台的SpringBoot和Cloud的升级&#xff0c;特次记录分享一下项目中可能出现的问题&#xff0c;方便后续的人快速定位问题。以及下述选择的解决方案都是基于让升级的服务影响和改动最小以及提供通用的解决方案的提前进行选择的。 1.1版本说明 升级前&a…

陇剑杯 省赛 攻击者1 CTF wireshark 流量分析

陇剑杯 省赛 攻击者1 题目 链接&#xff1a;https://pan.baidu.com/s/1KSSXOVNPC5hu_Mf60uKM2A?pwdhaek 提取码&#xff1a;haek ├───LogAnalize │ ├───linux简单日志分析 │ │ linux-log_2.zip │ │ │ ├───misc日志分析 │ │ acce…

Vue3项目 网易严选_学习笔记

Vue3项目 网易严选_第一天 主要内容 项目搭建vuex基础路由设计首页顶部和底部布局 学习目标 知识点要求项目搭建掌握vuex基础掌握路由设计掌握首页顶部和底部布局掌握 一、项目搭建 1.1 创建项目 vue create vue-wangyi选择vue3.0版本 1.2 目录调整 大致步骤&#xff…

Workerman开启ssl方法如下

参考地址 Workerman开启ssl方法如下-遇见你与你分享 准备工作&#xff1a; 1、Workerman版本不小于3.3.7 2、PHP安装了openssl扩展 3、已经申请了证书&#xff08;pem/crt文件及key文件&#xff09;放在了/etc/nginx/conf.d/ssl下 4、配置文件 location /wss { proxy_set…

unity制作拼接地图

前段时间有个朋友问我想要制作一款地图编辑器&#xff0c;最开始我还想着在一个平面用节点切割制作地图编辑器这倒是也行&#xff0c;但不太好控制每一个点&#xff0c;如果未来项目大了&#xff0c;更加不好维护。 偶然间翻到一篇文章&#xff1a;unity地图边缘检测 或许我们…

基于数字孪生的城市建模和仿真

近年来&#xff0c;数字孪生概念几乎呈爆炸式增长&#xff0c;利用该概念的科学文章数量呈指数级增长就证明了这一点。 这一概念源自制造业&#xff0c;使用 CAD 模型可以创建组件和产品的精确数字复制品。 该术语最早的使用可以追溯到 2003 年&#xff0c;通常归功于 Grieves …

vue3第二十节(新增编译宏defineModel)

为什么会需要使用defineModel() 注意&#xff1a;defineModel() 需要在3.4及以上版本才可使用&#xff1b; 组件之间通讯&#xff0c;通过 props 和 emits 进行通讯,是单向数据流&#xff0c;比如&#xff1a;props是自上而下的&#xff08;父组件数据修改导致子组件更新&…

生成人工智能体:人类行为的交互式模拟论文与源码架构解析(2)——架构分析 - 核心思想环境搭建技术选型

4.架构分析 4.1.核心思想 超越一阶提示&#xff0c;通过增加静态知识库和信息检索方案或简单的总结方案来扩展语言模型。 将这些想法扩展到构建一个代理架构&#xff0c;该架构处理检索&#xff0c;其中过去的经验在每个时步动态更新&#xff0c;并混合与npc当前上下文和计划…

【动态规划】切割钢条详解python

1. 问题介绍和应用场景 切割钢条问题是运筹学和算法设计中的一个经典问题&#xff0c;涉及如何最优化切割有限资源以最大化收益。这个问题经常用作动态规划教学的入门案例&#xff0c;同时在工业生产中也有实际应用&#xff0c;比如在金属加工业中如何切割原材料以减少浪费并增…

EelasticSearch是什么?及EelasticSearch的安装

一、概述 Elasticsearch 是一个基于 Apache Lucene 构建的开源分布式搜索引擎和分析引擎。它专为云计算环境设计&#xff0c;提供了一个分布式的、高可用的实时分析和搜索平台。Elasticsearch 可以处理大量数据&#xff0c;并且具备横向扩展能力&#xff0c;能够通过增加更多的…

Jmeter三个常用组件

Jmeter三个常用组件 一、线程组二、 HTTP请求三、查看结果树 线程组&#xff1a;jmeter是基于线程来运行的&#xff0c;线程组主要用来管理线程的数量&#xff0c;线程的执行策略。 HTTP请求&#xff1a;HTTP请求是jmeter接口测试的核心部分&#xff0c;主要使用HTTP取样器来发…

Android 12 如何加载 native 原生库

在 Android 7.0 及更高版本中&#xff0c;系统库与应用库是分开的。 图1. 原生库的命名空间 原生库的命名空间可防止应用使用私有平台的原生 API&#xff08;例如使用 OpenSSL&#xff09;。该命名空间还可以避免应用意外使用平台库&#xff08;而非它们自己的库&#xff09;的…

ES源码四:网络通信层流程

听说ES网络层很难&#xff1f;今天来卷它&#x1f604; 前言 ES网络层比较复杂&#xff0c;分为两个部分&#xff1a; 基于HTTP协议的REST服务端基于TCP实现的PRC框架 插件化设计的网络层模块&#xff08;NetworkModule&#xff09; 入口还是上一章的创建Node构造方法的地方…

【MySQL 安装与配置】Window简单安装MySQL,并配置局域网连接

文章日期&#xff1a;2024.04.17 系统&#xff1a;Window10 || Window11 类型&#xff1a;安装与配置MySQL数据库 文章全程已做去敏处理&#xff01;&#xff01;&#xff01; 【需要做的可联系我】 AES解密处理&#xff08;直接解密即可&#xff09;&#xff08;crypto-js.js…

系统稳定性建设

说到系统稳定性&#xff0c;不知道大家会想起什么&#xff1f;大多数人会觉得这个词挺虚的&#xff0c;不知道系统稳定性指的是什么。 一年前看到这个词&#xff0c;也是类似于这样的感受&#xff0c;大概只知道要消除单点、做好监控报警&#xff0c;但却并没有一个体系化的方…

记录一下我102连不上MySQL的问题 NotBefore

【背景描述】我在102上是能登录上MySQL的&#xff0c;但是用客户端&#xff08;DataGrip、SQLyog就连不上&#xff09; 【解决方案】 加个这个?useSSLfalse&serverTimezoneUTC 【另外的小问题】如果直接输mysql 上面这个不是报错&#xff0c;不用管 再输mysql -uroot -p…

upload-labs靶场详解

靶场环境 下载链接&#xff1a;https://codeload.github.com/c0ny1/upload-labs/zip/refs/heads/master 使用小皮集成环境来完成这个靶场 将文件放到WWW目录下就可以进行访问 进入关卡后页面呈现&#xff1a; Pass-01&#xff08;前端绕过&#xff09; 我们先尝试上传一个web.…

[svelte]属性和逻辑块

属性 / Default values • Svelte 教程 | Svelte 中文网 属性 Declaring props 到目前为止&#xff0c;我们只处理了内部状态——也就是说&#xff0c;这些值只能在给定的组件中访问。 在任何实际应用程序中&#xff0c;都需要将数据从一个组件向下传递到其子组件。为此&…

【Spring】-编程式事务和声明式事务

spring中控制事务的方式有两种&#xff1a;编程式事务和声明式事务&#xff0c;今天我以两种事务出发&#xff0c;对spring中实现事务的EnableTransactionManagement和Transaction两个注解的底层原理进行讨论。 一、编程式事务 什么是编程式事务&#xff1f; 硬编码的方式实现…