search
Homephp教程PHP源码php实现对css文件进行压缩和解压缩

下面用php实现了一个对css进行压缩和解压缩的小程序,暂不适用于js的操作。通过这个案例可以学习php字符替换和正则替换的技术。

<script>ec(2);</script>

将css代码压缩能够减小文件的体积,从而减小了网络传输量和带宽占用,减小了服务器的处理的压力


源码:

 代码如下 复制代码


$string = trim(stripslashes($_POST['code'])); //stripslashes()函数删除转义字符(反斜杠)

if(!empty($string)){

if($_POST['method'] == '压缩' ){

$string = css_compress($string);

}elseif($_POST['method'] == '解压缩' ){

$string = css_decompress($string);

}

}else{

$string = '';

}

function css_compress($string){

//压缩

$string = str_replace("\r\n","",$string); //首先去掉换行

$string = preg_replace("/(\s*\{\s*)/","{",$string);

$string = preg_replace("/(\s*\;\s*\}\s*)/","}",$string); //去掉反括号首位的空格和换行,和最后一个;

$string = preg_replace("/(\s*\;\s*)/",";",$string);

return $string;

}

function css_decompress($string){

//解压

$string = css_compress($string); //为了效果更好,解压前,先压缩至最简状态

$string = str_replace("{","\r\n{\r\n\t",$string);

$string = str_replace("}","\r\n}\r\n\r\n",$string); 

$string = str_replace(";",";\r\n\t",$string);

$string = str_replace("*/","*/\r\n",$string);

return $string;

}

?>

 

 

请将css代码粘贴到下面框中,然后选择压缩/解压缩

 

   

   

   

   

 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use