如何使用PHP删除JS注释?这个问题已更新:2013年11月4日回答:Alexander Yancharuk但是现在有一个问题.新代码:id = id.replace(/\//g,'');
这是我的例子:
<?php
$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 //
";
$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code
echo nl2br($output);
?>
Run Code Online (Sandbox Code Playgroud)
我的问题;
这是输出,最近:
this1
this2
this3
this4
this5 http://removecomment.com
ID = id.replace(/ \
Ale*_*ruk 19
试试这个:
$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 = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/';
$output = preg_replace($pattern, '', $output);
echo nl2br($output);
Run Code Online (Sandbox Code Playgroud)
在codepad.org上的结果.
Alexander Yancharuk的解决方案几近完美,他只是忘了忽略双引号,让他的答案在jquery.min.js上打破,其中包含代码:replace(Fb,yb [1] +"//")
请在下面找到更正后的版本:
$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 = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/';
$output = preg_replace($pattern, '', $output);
echo nl2br($output);
Run Code Online (Sandbox Code Playgroud)
我使用它来基于Manas Tungare的CSS示例在PHP中创建一个流动的JS压缩:http://manas.tungare.name/software/css-compression-in-php/
<?php
/**
* On-the-fly JS Compression by A. Heiligtag
* Based on On-the-fly CSS Compression by Manas Tungare.
*
* In order to minimize the number and size of HTTP requests for JS content,
* this script combines multiple JS files into a single file and compresses
* it on-the-fly.
*
*/
$cssFiles = array(
//<!-- Bootstrap -->
// <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
"./js/jquery-1.11.3.min.js",
// <!-- Bootstrap core JavaScript
"./js/bootstrap.min.js",
// <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
"./js/ie10-viewport-bug-workaround.js",
// <!-- Jasny bootstrap -->
"./js/jasny-bootstrap.min.js",
// <!-- color picker : https://github.com/istvan-ujjmeszaros/bootstrap-colorpickersliders / http://www.virtuosoft.eu/code/bootstrap-colorpickersliders/ -->
// <!-- ‘Polyglot’ Language Switcher 2 : http://www.ixtendo.com/polyglot-language-switcher-2/ -->
"./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('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $buffer);
// Remove space after colons
$buffer = str_replace(': ', ':', $buffer);
// Remove space before equal signs
$buffer = str_replace(' =', '=', $buffer);
// Remove space after equal signs
$buffer = str_replace('= ', '=', $buffer);
// Remove whitespace
$buffer = str_replace(array("\r\n\r\n", "\n\n", "\r\r", '\t', ' ', ' ', ' '), '', $buffer);
// Enable GZip encoding.
ob_start("ob_gzhandler");
// Enable caching
header('Cache-Control: public');
// Expire in one day
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
// Set the correct MIME type, because Apache won't set it for us
header("Content-type: application/javascript");
// Write everything out
echo($buffer);
?>
Run Code Online (Sandbox Code Playgroud)
如果您想从客户端 JavaScript 代码中删除注释,那么您就错了。
只需使用缩小器即可。除了删除注释之外,它还会删除无意义的空格并缩短脚本中的所有名称。
归档时间: |
|
查看次数: |
10360 次 |
最近记录: |