如何使用PHP删除JS注释? [英] How to remove JS comments using PHP?

查看:140
本文介绍了如何使用PHP删除JS注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP删除JS注释?
这个问题已更新:2013年11月4日回答:Alexander Yancharuk
但是现在有一个问题。一个新代码: id = id.replace(/ \ // g,'');

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.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(/ \



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

这篇关于如何使用PHP删除JS注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆