12

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. this1 行に問題があります。
  2. //コメントは機能していますが、/* コメント */ を削除するコードを作成できないか、そのコメントに改行を追加できません

最近の出力は次のとおりです。



this1
this2
this3
this4
this5 http://removecomment.com
id = id.replace(/\

4

7 に答える 7

21

これを試して:

$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での結果。

于 2013-10-22T06:34:46.570 に答える
1

これは私の最高の結果です。最初の投稿に続いて、多くのサイトを検索しました。この関数は、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;
}
于 2015-04-12T12:51:20.963 に答える
1

JavaScript コメントと CDATA ブロックを削除するには(//<![CDATA[ , //]]>)、alex のソリューション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);
于 2014-06-04T07:59:36.727 に答える
0

クライアント側の JavaScript コードからコメントを削除したい場合は、間違っています。

ミニファイヤを使用するだけです。コメントを削除するだけでなく、意味のない空白を削除し、スクリプト内のすべての名前を短縮します。

: github: UglifyJS 2

于 2013-10-22T05:59:17.810 に答える
-2

私が自分用に書いたコードの正規表現 (PHP) スニペットを共有したいと思います。これは、単純なコードとしてマークされた joomla の YIREO sriptmerge プラグインでも使用されています。JavaScript コードを圧縮し、そこからすべてのコメントを削除します。mootools でも動作します。(他の PHP ソリューションと比較して) 高速で、JavaScript 自体に損傷を与えることはなく、多くのコメント削除の問題を解決します。

If you click on the link below you find a comment removal script in regex. 

これらは、mootools、Joomla、drupal、およびその他の cms Web サイトと連携して動作する 112 行のコードです。800.000 行のコードとコメントでテストしました。正常に動作します。これは、( abc(/ nn /('/ xvx /'))"// testing line") のような複数の括弧と、コロンの間にあるコメントも選択して保護します。2016 年 1 月 23 日..! これはコメントを含むコードです.!!!!

ここをクリック

var regex=/(ftp|https?):\/\//;alert('hello,world');楽しみのために、これ ( ) とこれ /* foo(); // some comment */とこれをテストしました。"Python's division: 1 // 2" この//"you dope"例と私の圧縮コードは、上記の例に影響を与えません! 上記のコメント:( 正規表現を使用する愚かなパーサーは、有効な JavaScript コードをコメントとして扱います!) したがって、正規表現を使用して愚かなパーサーを作成することもできますか?

有効な JavaScript 文を使用してこのスクリプトに問題を見つけた場合は、お気軽にコメントを残してください。また、正しく解決されない、結合された (または結合されていない) あらゆる種類のコメント タグ..

更新されたので、これ以上テストする必要はありません。800.000 行が正常に動作するようになりました。

于 2015-01-31T23:31:39.597 に答える