Regex to remove line from css file

I am trying to remove include statements from a css file. So given a css file for example

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

after running the command I want to be

.myfirstclass {color:red}

This is the command I use, but it does not work. Is there any way to do this?

$css_file = preg_replace("/^@import url(.*)$/", "", $css_file);    
+4
source share
1 answer

Caret, ^together with the dollar sign, $means approving the beginning of the input line and its end if the flag is not set m. You also need to check for spaces at the beginning of a line and matches for lines at the end:

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

In case of working with mini CSS:

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

Source: https://habr.com/ru/post/1693460/


All Articles