Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Combine preg_replace_callback in function

  • 06-02-2011 06:19PM
    #1
    Closed Accounts Posts: 4


    Hello everyone.

    I got the following functions:
    function html_compress($html)
    {
        preg_match_all('!(<(?:code|pre).*>[^<]+</(?:code|pre)>)!',$html,$pre);#exclude pre or code tags
           
        $html = preg_replace('!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#pre#', $html);#removing all pre or code tags
        $html = preg_replace('#<!&#8211;[^\[].+&#8211;>#', &#8221;, $html);#removing HTML comments
        $html = preg_replace('/[\r\n\t]+/', ' ', $html);#remove new lines, spaces, tabs
        $html = preg_replace('/>[\s]+</', '><', $html);#remove new lines, spaces, tabs
        $html = preg_replace('/[\s]+/', ' ', $html);#remove new lines, spaces, tabs
           
        if(!empty($pre[0]))
            foreach($pre[0] as $tag)
                       $html = preg_replace('!#pre#!', $tag, $html,1);#putting back pre|code tags
           
              return $html;
           
    }
    
    
    function css_minify( $css ) {
        $css = preg_replace( '#\s+#', ' ', $css );
        $css = preg_replace( '#/\*.*?\*/#s', '', $css );
        $css = str_replace( '; ', ';', $css );
        $css = str_replace( ': ', ':', $css );
        $css = str_replace( ' {', '{', $css );
        $css = str_replace( '{ ', '{', $css );
        $css = str_replace( ', ', ',', $css );
        $css = str_replace( '} ', '}', $css );
        $css = str_replace( ';}', '}', $css );
    
        return trim( $css );
    }
    

    I also got the following regex to detect text inside <style> tags.
    preg_match_all('/<style.*>(.*)<\/style>/isU', $html, $matches);
    

    I would like to combine the regex inside the html_compress so everywhere there's <style> tag, it will send the text inside the style tag to the css_minify function.

    I tried to use preg_replace_callback() >
    preg_replace_callback ('/<style.*>(.*)<\/style>/isU', 'css_minify', $html);
    

    located it as first line in html_compress but it didnt work. I have no idea if its the right location plus, I am pretty sure I should add/change something at the css_minify function but I have no idea what...

    Can you help me?


    Thanks!


Comments

  • Registered Users, Registered Users 2 Posts: 354 ✭✭AndrewMc


    I haven't looked at your regular expressions, but you're right about needing to tweak css_minify. The callback is sent an array, not a single string, so you'll need to refer to $css[0] rather than just $css.


  • Closed Accounts Posts: 4 chubbyz


    AndrewMc wrote: »
    I haven't looked at your regular expressions, but you're right about needing to tweak css_minify. The callback is sent an array, not a single string, so you'll need to refer to $css[0] rather than just $css.

    yes, it did the job. Thank you.


Advertisement