簡體   English   中英

如何使用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);
?>

我的問題;

  1. 這一行有問題;
  2. //注釋正在運行,但我無法創建代碼來刪除/ *注釋* /或帶有換行符的注釋

這是輸出,最近:



this1
this2
this3
this4
this5 http://removecomment.com
id = 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);

我使用它來基於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);
?>

要刪除JavaScript注釋和CDATA塊(//<![CDATA[ , //]]>)結合Alex的soluton https://stackoverflow.com/a/8283600/2910183和亞歷山大的Yancharuk解決方案https://stackoverflow.com/ a / 19510664/2910183

$content = preg_replace('~//<!\[CDATA\[\s*|\s*//\]\]>~', '', $content);
$content = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\)\/\/[^"\'].*))/', '', $content);

這是我的最好的結果,在第一篇文章和搜索許多網站后,這個功能適用於PHP中的JavaScript和CSS和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;
}

如果您想從客戶端javascript代碼中刪除評論,那么您做錯了。

只需使用minifier。 除了刪除注釋之外,它還將刪除無意義的空格並縮短腳本中的所有名稱。

例如github:UglifyJS 2

復雜的正則表達式掛起!!! 使用簡單的工具可以使用數千行

$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

我想分享一個我為自己寫的代碼的正則表達式(PHP)代碼段,它也被用在joomla的YIREO sriptmerge插件中,標記為簡單代碼。壓縮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 */ /* foo(); // some comment */和這個: "Python's division: 1 // 2"和這個//"you dope"例子和我的壓縮代碼那些不會破壞上面的例子! 上面的評論:(使用正則表達式的愚蠢解析器會將有效的JavaScript代碼視為注釋!)所以也許你也可以用正則表達式編寫沒有愚蠢的解析器???

如果您發現此腳本存在問題且有效的javascript語句,請隨時發表評論。 任何類型的評論標簽組合(或不組合),它沒有正確解決..

更新的需求不再需要測試800.000線現在工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM