PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Victor V. Nabatov   PHP CSS Optimizer   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: PHP CSS Optimizer
Optimize CSS and make it browser independent
Author: By
Last change: Update
Date: 8 years ago
Size: 1,042 bytes
 

Contents

Class file image Download
<?php
#
# Remove all files from CACHE directory and put your browser to http://localhost/css-optimizer/example.php
#
ini_set('display_errors', 1); // For testing
define ('DS', DIRECTORY_SEPARATOR);
define ('CACHE', 'cache'.DS);
define ('CONFIG', 'assets'.DS.'config.ini');

require_once
'css.class.php';
$html = file_get_contents('example.html');

preg_match_all("#\<link rel=\"stylesheet\" type=\"text\/css\" href=\"(.*?)\" media=\"(.*?)\" \/\>#is", $html, $match, PREG_SET_ORDER);

foreach(
$match as $key => $css) {
   
$CSS = new CSS(['cache_css' => TRUE]);
   
$html = str_replace($css[0], '<style type="text/css">'.$CSS->compress($css[1]).'</style>', $html);
}

preg_match_all("#\s*<style\b[^>]*?>\s*<!--\s*([\s\S]*?)\s*-->\s*<\/style>\s*#i", $html, $match, PREG_SET_ORDER);
foreach(
$match as $key => $css) {
   
$CSS = new CSS(['cache_css' => FALSE]); // Embedded styles sheet will be cached with page so FALSE
   
$html = str_replace($css[0], '<style type="text/css">'.$CSS->compress($css[1]).'</style>', $html);
}

echo
$html;