Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Combine preg_replace_callback in function

Options
  • 06-02-2011 7: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 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