How to remove JS comments using PHP?

前端 未结 7 1345
慢半拍i
慢半拍i 2020-12-09 12:02

How to remove JS comments using PHP? This question is updated: Nov. 4 2013 and answered by: Alexander Yancharuk But there is a problem right now. A new code: id = id.r

7条回答
  •  [愿得一人]
    2020-12-09 12:38

    Alexander Yancharuk solution is nearly perfect, he just forgot to ignore double quotes, making his answer break on jquery.min.js which contains the code : replace(Fb,yb[1]+"//")

    Please find below the corrected version :

    $output = "
    //remove comment
    this1 //remove comment
    this2 /* remove comment */
    this3 /* remove
    comment */
    this4 /* * * remove
    * * * *
    comment * * */
    this5 http://removecomment.com
    id = id.replace(/\//g,''); //do not remove the regex //
    HTTP+'//www.googleadservices.com/pagead/conversion'
    ";
    
    $pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?

    I used this to create a on-the-flow JS compression in PHP based on Manas Tungare example for CSS : http://manas.tungare.name/software/css-compression-in-php/

    
        // 
        "./js/jquery-1.11.3.min.js",
        // 
        "./js/ie10-viewport-bug-workaround.js",
    
         // 
        "./js/jasny-bootstrap.min.js",
    
        // 
        // 
        "./js/jquery-polyglot.language.switcher.js"
    
    );
    
    /**
     * Ideally, you wouldn't need to change any code beyond this point.
     */
    $buffer = "";
    foreach ($cssFiles as $cssFile) {
      $buffer .= file_get_contents($cssFile);
    }
    // Remove comments
    $buffer = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?
    

提交回复
热议问题