About how to compress CSS code in php

jacklove
Release: 2023-03-27 11:14:02
Original
1590 people have browsed it

This article will explain in detail how to compress CSS code in PHP.

Place the following code in the style.css.php file. Don’t forget to include the css files you need:

<?php
header(&#39;Content-type: text/css&#39;);ob_start("compress");function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace(&#39;!/\*[^*]*\*+([^/][^*]*\*+)*/!&#39;, &#39;&#39;, $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("
", "\r", "\n", "\t", &#39;  &#39;, &#39;    &#39;, &#39;    &#39;), &#39;&#39;, $buffer);
  return $buffer;}
 /* your css files */include(&#39;master.css&#39;);include(&#39;typography.css&#39;);include(&#39;grid.css&#39;);include(&#39;print.css&#39;);include(&#39;handheld.css&#39;);
 ob_end_flush();
Copy after login

Then introduce the php file in the HTML page where the style is introduced:

<link rel="stylesheet" type="text/css" media="screen" href="style.css.php"/>
Copy after login

This article explains how to compress CSS code in PHP. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

PHP receives json and inserts the received data into the database

PHP MySql paging example

Processing Protocol Buffers data in PHP

The above is the detailed content of About how to compress CSS code in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template