preg_replace out CSS comments?

后端 未结 4 580
一整个雨季
一整个雨季 2020-12-10 16:12

I\'m writing a quick preg_replace to strip comments from CSS. CSS comments usually have this syntax:

/* Development Classes*/
/* Un-comment me for easy testi         


        
4条回答
  •  北海茫月
    2020-12-10 17:06

    I had the same issue. To solve it, I first simplified the code by replacing "/ASTERIX" and "ASTERIX/" with different identifiers and then used those as the start and end markers.

    $code = str_replace("/*","_COMSTART",$code);
    $code = str_replace("*/","COMEND_",$code);
    $code = preg_replace("/_COMSTART.*?COMEND_/s","",$code);
    

    The /s flag tells the search to go onto new lines

提交回复
热议问题