PHP Classes

File: engine/class.www-minifier.php

Recommend this page to a friend!
  Classes of Kristo Vaher  >  Wave Framework  >  engine/class.www-minifier.php  >  Download  
File: engine/class.www-minifier.php
Role: Class source
Content type: text/plain
Description: Minifier Class
Class: Wave Framework
MVC framework for building Web sites and APIs
Author: By
Last change: Added subkeyword parameter to getTranslations() method in case translations are grouped in translations files. Added getContent() method that can be used to load static language-based HTML within Views from /resources/content/ folder and subfolders. Added 'server-name' and 'server-port' State variables.
Date: 8 years ago
Size: 3,285 bytes
 

Contents

Class file image Download
<?php

/**
 * Wave Framework <http://www.waveframework.com>
 * Minifier Class
 *
 * Minifier is used to minify text data without breaking functionality of that text. This is
 * useful to be applied to JavaScript, CSS Stylesheets, HTML and XML formats for purposes of
 * making the file size smaller, thus increasing the performance when file is transferred over
 * HTTP. You should be careful when using minifier however, since it might break functionality
 * under some instances, so it is always good to test before deploying minified resources to
 * live systems.
 *
 * @package Minifier
 * @author Kristo Vaher <kristo@waher.net>
 * @copyright Copyright (c) 2012, Kristo Vaher
 * @license GNU Lesser General Public License Version 3
 * @tutorial /doc/pages/minifier.htm
 * @since 1.7.0
 * @version 3.4.8
 */

class WWW_Minifier {
   
   
/**
     * This method removes comments, tabs, spaces, new-lines and various other spaces from
     * text. It assumes that text is in a CSS-like format.
     *
     * @param string $data data string to be minified
     * @return string
     */
   
public static function minifyCSS($data){
   
       
// Removing comments
       
$data=preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$data);
       
// Removing tabs, spaces and newlines
       
$data=str_replace(array("\r\n","\r","\n","\t",' ',' ',' '), '', $data);
       
// Removing other spaces before and after
       
$data=preg_replace(array('(( )+{)','({( )+)'),'{',$data);
       
$data=preg_replace(array('(( )+})','(}( )+)','(;( )*})'),'}',$data);
       
$data=preg_replace(array('(;( )+)','(( )+;)'),';',$data);
       
//Returning minified string
       
return $data;
       
    }
   
   
/**
     * This method removes comments, tabs, spaces, new-lines and various other spaces from
     * text. It assumes that text is in a JavaScript-like format.
     *
     * @param string $data data string to be minified
     * @return string
     */
   
public static function minifyJS($data){
   
       
// Removing comments
       
$data=preg_replace('/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/','',$data);
       
// Removing tabs and newlines
       
$data=str_replace(array("\r\n","\r","\n"),'',$data);
       
// Removing whitespace
       
$data=preg_replace('/\s+/', ' ',$data);
       
//Returning minified string
       
return $data;
       
    }
   
   
/**
     * This method removes comments, tabs, spaces, new-lines and various other spaces from
     * text. It assumes that text is in a HTML-like format.
     *
     * @param string $data data string to be minified
     * @return string
     */
   
public static function minifyHTML($data){
   
       
// Remove newlines and tabs
       
$data=preg_replace('/[\r\n\t]/i','',$data);
       
// Remove comments
       
$data=preg_replace('/<!--.*?-->/i','',$data);
       
//Returning minified string
       
return $data;
       
    }
   
   
/**
     * This method removes comments, tabs, spaces, new-lines and various other spaces from
     * text. It assumes that text is in a XML-like format.
     *
     * @param string $data data string to be minified
     * @return string
     */
   
public static function minifyXML($data){
   
       
// Remove newlines and tabs
       
$data=preg_replace('/[\r\n\t]/i','',$data);
       
// Remove comments
       
$data=preg_replace('/<!--.*?-->/i','',$data);
       
//Returning minified string
       
return $data;
       
    }
   
}
   
?>

For more information send a message to info at phpclasses dot org.