IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Less-CSS

Do soutěže Machr na PHP

php

<?php

/*
 * POUŽITÍ:
 *  require "./LEcSS.php";
 *  $lessCss = new LEcSS('style.css');
 *  $lessCss->printCss();
 */

class LeCSS
{
  protected $css;
  protected $cssText;
  protected $allCss;

  public function __construct($path)
  {
    // Existuje zdroj?
    if(file_exists($path))
    {
      // Soubor nebo složka
      if(is_file($path))
      {
        $this->css = file_get_contents($path);
        $this->saveText();
        $this->deleteUselessChar();
        $this->recoverText();
        $this->allCss = $this->css;
      }
      else
      {
        //složka
        $dir = opendir($path);
        while($file = readdir($dir)){
          if(substr($file, strlen($file)-3, 3) == 'css')
          {
            $this->css = file_get_contents($path.'/'.$file);
            $this->saveText();
            $this->deleteUselessChar();
            $this->recoverText();
            $this->allCss .= $this->css;
          }
        }
      }
    }
    elseif(is_array($path))
    {
      //pole
      foreach($path as $file)
      {
        $this->css = file_get_contents($file);
        $this->saveText();
        $this->deleteUselessChar();
        $this->recoverText();
        $this->allCss .= $this->css;
      }
    }
  }

  function saveText()
  {
    $textIndex = 0;

    $length = strlen($this->css);

    for($i = 0; $i < $length; $i++)
    {
      if($this->css[$i] == '"')
      {
        $this->cssText[$textIndex] = '';
        for($j = $i + 1; $j < $length and $this->css[$j] != '"'; $j++)
        {
          $this->cssText[$textIndex] .= $this->css[$j];
          $this->css[$j] = '';
        }
        $i = $j;
        $textIndex += 1;
      }
      if($this->css[$i] == '\'')
      {
        $this->cssText[$textIndex] = '';
        for($j = $i + 1; $j < $length and $this->css[$j] != '\''; $j++)
        {
          $this->cssText[$textIndex] .= $this->css[$j];
          $this->css[$j] = '';
        }
        $i = $j;
        $textIndex += 1;
      }
    }
  }

  function recoverText()
  {
    $textIndex = 0;

    $length = strlen($this->css);

    $in = true;

    for($i = 0; $i < $length; $i++)
    {
      if($this->css[$i] == '\'' or $this->css[$i] == '"')
      {
        if($in)
        {
          $this->css = substr_replace($this->css, $this->cssText[$textIndex], $i + 1, strlen($this->cssText[$textIndex]));
          $length = strlen($this->css);
          $in = false;
          $textIndex += 1;
        }
        else
        {
          $in = true;
        }
        
      }
    }
  }

  function deleteUselessChar()
  {
    $this->css = preg_replace(
    array("/(\/\*(.+?)\*\/)|(\r\n|\n|\r)/", "/[ ]+/", "/[ ]*;[ ]*/", "/[ ]*:[ ]*/", "/[ ]*,[ ]*/", "/[ ]*\{[ ]*/"),
    array(null, ' ', ';', ':', ',', '{'), $this->css);
    $this->css = str_replace(';}', '}', $this->css);
  }

  public function printCss()
  {
    header("Content-type: text/css");
    print $this->allCss;
  }
}

Neformátovaný

Přidáno: 13.10.2013
Expirace: Neuvedeno

Avatar
Autor: FastNode
Aktivity