Php implements compression and decompression of css files-PHP source code

Source: Internet
Author: User
Below we use php to implement a small program for css compression and decompression, which is not suitable for js operations for the moment. In this case, you can learn the php character replacement and regular expression Replacement Technologies. Below we use php to implement a small program for css compression and decompression, which is not suitable for js operations for the moment. In this case, you can learn the php character replacement and regular expression Replacement Technologies.

Script ec (2); script

Compressing css code reduces the file size, reduces the amount of network transmission and bandwidth usage, and reduces the processing pressure on the server.


Source code:

The Code is as follows:


$ String = trim (stripslashes ($ _ POST ['code']); // The stripslashes () function deletes escape characters (backslash)

If (! Empty ($ string )){

If ($ _ POST ['method'] = 'compressing '){

$ String = css_compress ($ string );

} Elseif ($ _ POST ['method'] = 'decompressed '){

$ String = css_decompress ($ string );

}

} Else {

$ String = '';

}

Function css_compress ($ string ){

// Compression

$ String = str_replace ("\ r \ n", "", $ string); // remove the line feed first.

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

$ String = preg_replace ("/(\ s * \; \ s * \} \ s *)/", "}", $ string ); // remove the spaces and line breaks at the beginning of the parentheses and the last line;

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

Return $ string;

}

Function css_decompress ($ string ){

// Extract

$ String = css_compress ($ string); // before decompression, compress to the simplest state.

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

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

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

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

Return $ string;

}

?>

Paste the css Code in the following box and select compression/Decompression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.