user3052443
user3052443

Reputation: 854

Regex to remove line from css file

I'm trying to remove include statements from a css file. So given a css file like

@import url("css1.css");
@import url("css2.css");
@import url("css3.css");
.myfirstclass {color:red}

after I run the command I want to be

.myfirstclass {color:red}

This is the command I am using but it isn't working. Is there a way to do this?

$css_file = preg_replace("/^@import url(.*)$/", "", $css_file);    

Upvotes: 4

Views: 260

Answers (1)

revo
revo

Reputation: 48761

Caret ^ alongside dollar sign $ mean asserting start of input string and end of it respectively unless m flag is set. You also need to check for spaces in beginning of line and match linebreaks at end:

$css_file = preg_replace("/^\s*@import url.*\R*/m", "", $css_file);  
                             ^               ^  ^

In case of working with minified CSS:

$css_file = preg_replace("/@import[^;]+;/", "", $css_file);  

Upvotes: 2

Related Questions