[php] How to make function for compress css using regular expression

Written by  Tuesday, 24 October 2017 20:04
function compresscss ( $data, $url ) {
        global $current_css_url;
        $current_css_url = $url;
        /* remove comments */
        $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
        /* remove tabs, spaces, new lines, etc. */
        $data = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), ' ', $data);
        /* remove unnecessary spaces */
        $data = preg_replace('/[ ]+([{};,:])/', '\1', $data);
        $data = preg_replace('/([{};,:])[ ]+/', '\1', $data);
        /* remove empty class */
        $data = preg_replace('/(\}([^\}]*\{\})+)/', '}', $data);
        /* remove PHP code */
        $data = preg_replace('/<\?(.*?)\?>/mix', '', $data);
        /* replace url*/
        $data = preg_replace_callback('/url\(([^\)]*)\)/', 'replaceurl', $data);
        return $data;


251 Read

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.


  • Comment Link cheap jordans Monday, 12 February 2018 10:54 cheap jordans

    When I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a remark is added I get four emails with the identical comment. Is there any manner you possibly can take away me from that service? Thanks!

  • Comment Link nike polo shirts Wednesday, 14 February 2018 04:54 nike polo shirts

    Spot on with this write-up, I truly think this web site wants rather more consideration. I抣l probably be once more to learn far more, thanks for that info.

  • Comment Link yeezy boost 350 v2 Saturday, 17 February 2018 01:12 yeezy boost 350 v2

    I precisely wished to appreciate you once more. I'm not certain what I might have handled in the absence of those secrets discussed by you relating to my problem. It absolutely was a very frightening setting in my view, however , observing a expert strategy you managed that forced me to weep with gladness. I will be thankful for the assistance and then wish you realize what a great job you are accomplishing training people through the use of a site. I am certain you've never encountered any of us.

  • Comment Link air jordan Monday, 19 February 2018 11:29 air jordan

    I found your weblog site on google and test a couple of of your early posts. Proceed to maintain up the very good operate. I just extra up your RSS feed to my MSN News Reader. Looking for forward to studying extra from you in a while!?

  • Comment Link yeezy shoes Wednesday, 28 February 2018 02:33 yeezy shoes

    I together with my pals came going through the best techniques located on the website while all of a sudden came up with a terrible feeling I had not expressed respect to the blog owner for those secrets. Those people were absolutely passionate to read all of them and already have honestly been taking advantage of these things. We appreciate you truly being very helpful as well as for deciding upon these kinds of useful subject matter millions of individuals are really desirous to discover. Our own sincere apologies for not expressing appreciation to sooner.

  • Comment Link ultra boost Tuesday, 06 March 2018 02:45 ultra boost

    Youre so cool! I dont suppose Ive read something like this before. So nice to search out any person with some original thoughts on this subject. realy thank you for beginning this up. this web site is something that's wanted on the net, somebody with just a little originality. helpful job for bringing something new to the web!

Related items

  • [solved] How to delete text symbol from php line?
    in PHP
    $string = 'Some Text .... 1. 2 .3.  Buy';
    $string = preg_replace('~[^0-9]+~','',$string);
    echo $string;
    // 123 
  • How to show all errors in php?
    in PHP

    If you need show some errors in your php code - use this php notation:

    // enable showing errors in PHP
    ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);
  • Joomla! 4 – The hope of promising year 2018

    During 2017, we have heard a lot of opinions that this year has been too gloomy for Joomla. However, we think that this year is so important because it is the stepping stone for the explosion in 2018 when we prepare to welcome Joomla! 4.0 and Joomla! Framework 2.0. In addition to introducing several new features, including a fully rebuilt Media Manager, an enhanced event dispatching system, and new security features such as support for prepared SQL statements, we have also been doing some routine maintenance and paying off some of our project’s technical debt by retiring deprecated code and raising the minimum supported software stack. And, with this released version, we also require PHP 7.

  • How to trim String text in php
    in PHP

    For example we have text

                                The Times

     But good result need be

    <strong>The Times NY</strong>

     I make regexp in php

    function trim_text($str)                             
        return trim(preg_replace('/\s{2,}/', ' ', $str));                                                 


  • Mac how to Upgrade php v5 to php v7
    in PHP

    We will proceed by installing PHP 5.5, PHP 5.6, PHP 7.0, and PHP 7.1 and using a simple script to switch between them as we need.

    $ brew install php55 --with-apache
    $ brew unlink php55
    $ brew install php56 --with-apache
    $ brew unlink php56
    $ brew install php70 --with-apache
    $ brew unlink php70
    $ brew install php71 --with-apache

     Let's do it!

    brew install php71

    But, we looking Error:

    ==> Installing php71 from josegonzalez/php
    Error: Cannot install josegonzalez/php/php71 because conflicting formulae are installed.
      php55: because different php versions install the same binaries.
    Please `brew unlink php55` before continuing.
    Unlinking removes a formula's symlinks from /usr/local. You can
    link the formula again after the install finishes. You can --force this
    install, but the build may fail or cause obscure side-effects in the
    resulting software.

    Dont panic! :-) Run unlink command:

    brew unlink php55

    # Unlinking /usr/local/Cellar/php55/5.5.38_11... 17 symlinks removed

    Then try again:

    brew install php71
    # ==> Summary
    # ?  /usr/local/Cellar/php71/7.1.0_11: 342 files, 39.7M

    Then try our php version

    php -v

    PHP 7.1.0 (cli) (built: Dec  2 2016 03:30:24) ( NTS )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies