How to remove JS comments using PHP?

前端 未结 7 1343
慢半拍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:49

    This is my best result, following the first post and searching many sites, this function well works for javascript and css and html content in php

    function _compress($html){
       //remove comments
       $html = preg_replace('|(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))|', '', $html ); //Yancharuk's code/regex
    
       // remove tabs, spaces, newlines, etc.
       $html = str_replace(array(PHP_EOL, "\t"), '', $html);
    
       //remove all spaces
       $html = preg_replace('|\s\s+|', ' ', $html);
    
       return $html;
    }
    
    0 讨论(0)
提交回复
热议问题