preg_replace out CSS comments?

后端 未结 4 590
一整个雨季
一整个雨季 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 16:56

    Just for fun(and small project of course) I made a non-regexp version of a such code (I hope it's faster):

    function removeCommentFromCss( $textContent )
    {
        $clearText = "";
        $charsInCss = strlen( $textContent );
        $searchForStart = true;
        for( $index = 0; $index < $charsInCss; $index++ )
        {
            if ( $searchForStart )
            {
                if ( $textContent[ $index ] == "/" && (( $index + 1 ) < $charsInCss ) && $textContent[ $index + 1 ] == "*" )
                {
                    $searchForStart = false;
                    continue;
                }
                else
                {
                    $clearText .= $textContent[ $index ];
                }
            }
            else
            {
                if ( $textContent[ $index ] == "*" && (( $index + 1 ) < $charsInCss ) && $textContent[ $index + 1 ] == "/" )
                {
                    $searchForStart = true;
                    $index++;
                    continue;
                }
            }
        }
        return $clearText;
    }
    

提交回复
热议问题