如何使用 PHP 删除 JS 注释
How to remove JS comments using PHP?
如何使用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);
?>
我的问题;
- this1 行有问题; //
- 注释正在工作,但我无法创建代码来删除/*注释 */或带有换行符的注释
这是最近的输出:
这1 这2 这3 这4 这5 http://removecomment.comid = id.replace(/''
试试这个:
$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);
结果在 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);
我用它来创建一个基于CSS的Manas Tungare示例的PHP中的流动JS压缩 http://manas.tungare.name/software/css-compression-in-php/:
<?php
/**
* On-the-fly JS Compression by Amael H.
* 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);
?>
复杂的正则表达式挂起!!使用简单的来处理数千行
$module = preg_replace('/'/'*[^'/]*'*'//', '', $module);
/*
* remove comments like this
*/
$module = preg_replace('/'/'*'*(('r'n|'n) '*[^'n]*)+('r'n|'n) '*'//', '', $module);
/**
* remove comments like this other method
*/
$module = preg_replace('/'n('s+)?'/'/[^'n]*/', '', $module);
// remove comments like this
$module = preg_replace('/ ('t| )+/', '', $module);
//double spaces
$module = preg_replace('/(['n])+/', "$1", $module);
//double newlines
要删除 JavaScript 注释和 CDATA 块,(//<![CDATA[ , //]]>)
将 alex 的 soluton https://stackoverflow.com/a/8283600/2910183 和 Alexander 的 Yancharuk 解决方案组合在一起 https://stackoverflow.com/a/19510664/2910183:
$content = preg_replace('~//<!'[CDATA'['s*|'s*//']']>~', '', $content);
$content = preg_replace('/(?:(?:'/'*(?:[^*]|(?:'*+[^*'/]))*'*+'/)|(?:(?<!':|''')'/'/[^"''].*))/', '', $content);
从客户端javascript代码中删除注释,那么你就做错了。
只需使用缩小器。除了删除注释之外,它还将删除无意义的空格并缩短脚本中的所有名称。
例如 github:UglifyJS 2
这是我最好的结果,在第一篇文章并搜索许多网站之后,这个函数适用于JavaScript和CSS以及PHP中的HTML内容
。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;
}
分享一个正则表达式(PHP)代码片段我为自己写了它也被用于YIREO sriptmerge插件为joomla标记为简单代码。压缩 JavaScript 代码并从中删除所有注释。它也适用于mootools它很快(与其他PHP解决方案相比)并且不会损坏JavaScript 它自己解决了,它解决了很多注释删除问题。
If you click on the link below you find a comment removal script in regex.
这些是112行代码,可以协同工作,也可以与mootools,Joomla和drupal以及其他cms网站一起使用。在 800.000 行代码和注释上对其进行了测试。工作正常。这个还选择了多个括号,如(abc(/nn/('/xvx/')))"//测试行")和冒号之间的注释并保护它们。23-01-2016..!这是包含注释的代码!!!
点击这里
我只是为了好玩而测试了这个( var regex=/(ftp|https?):'/'//;alert('hello,world');
) 和这个 /* foo(); // some comment */
和这个:"Python's division: 1 // 2"
而这个//"you dope"
的例子我的压缩代码不会损坏上面的例子!上面的评论:(使用正则表达式的愚蠢解析器会将有效的JavaScript代码视为注释!所以也许你也可以用正则表达式编写不愚蠢的解析器???
如果您发现此脚本与有效的 JavaScript 句子有问题。和任何种类的评论标签组合(或未组合)它不能正确解决。.
更新后无需再测试 800.000 行现在工作正常。!
- 删除Angular JS中的辅助HTML注释
- 使用gruntjs注释或取消注释js块
- 浏览器注释掉使用JS插入的代码
- 正则表达式在 JS 文件中查找第一个注释
- 正确匹配JS多行注释的每一行
- 如何使用 PHP 删除 JS 注释
- 如何使用 JS/jQuery 在 IE 中捕获注释 nodeType 8
- 隐藏的代码注释,甚至在源代码(HTML / JS)中
- 使用 GCC 连接多个 JS 模块,从 ES2015 转译并注入带有 @define 注释的变量
- Vue.js 注释中的绑定
- 是否可以使用 JS 或其他 PL 将注释包含在注释中
- Node.js regex:在样式表中查找所有CSS属性,而不是在注释中
- 获取dust.js以剥离JavaScript注释
- HTML SELECT JS onchange()禁用注释框
- JS库:what's的注释块中带有感叹号
- 在PHP中转义json数据而不使用JS注释hack的正确方法
- 如何添加一个显示/隐藏注释链接不工作与下划线.js
- 使用sweet.js生成动态注释
- .js注释中的每个绑定没有按预期工作
- 使用angular.JS注释网页