Home  >  Article  >  Backend Development  >  About how to compress CSS code in php

About how to compress CSS code in php

jacklove
jackloveOriginal
2018-05-22 10:36:351545browse

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();

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"/>

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!

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