Stripping HTML Comments With PHP But Leaving Conditionals

后端 未结 5 1722
夕颜
夕颜 2020-12-30 14:47

I\'m currently using PHP and a regular expression to strip out all HTML comments from a page. The script works well... a little too well. It strips out all comments includin

5条回答
  •  一个人的身影
    2020-12-30 15:28

    If you can't get it to work with one regular expression or you find you want to preserve more comments you could use preg_replace_callback. You can then define a function to handle the comments individually.

    /U', 'comment_replace_func', $buffer);
    }
    
    function comment_replace_func($m) {
        if (preg_match( '/^\<\!--\[if \!/i', $m[0])) {
            return $m[0];   
        }              
    
        return '';
    }   
    
    ob_start("callback");
    ?>
    
    ... HTML source goes here ...
    
    
    

提交回复
热议问题