Regular expression to remove CSS comments

前端 未结 1 739
情话喂你
情话喂你 2020-12-06 13:31

I want to write the regular expression in php for matching the line within a double and single quotes. Actually I am writing the code for removing comment lines in css file.

相关标签:
1条回答
  • 2020-12-06 13:44

    The following should do it:

    preg_replace( '/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/' , '' , $theString );
    

    Test case:

    $theString = '- valid code /* comment */ next valid code "/* not a comment */" /* this is comment */';
    
    preg_replace( '/(?!<\")\/\*[^\*]+\*\/(?!\")/' , ' ' , $theString );
    
    # Returns 'valid code next valid code "/* not a comment */" '
    

    Revision : 28 Nov 2014

    As per comments from @hexalys, who referred to http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php

    The updated regular expression, as per that article, is:

    preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!' , '' , $theString );
    
    0 讨论(0)
提交回复
热议问题