PHP-php做一个程序高效去除注释的方法

发布于 2017-02-08 00:48:36 字数 129 浏览 1510 评论 4

需要获取一份无注释的的源代码提供给第三方,因为做了程序使用的详细手册,所以不希望代码中再含有注释,这个是特殊的而且是硬性的要求,没有其他可商量的,希望可以有跳过某些文件夹的功能,尽量兼容C/C++的注释风格,如果可以起到一定压缩作用的那就最好了

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚风撩人 2017-08-30 12:13:15

这里我结合手册的方法改造了一个方法,本人测试避免了@lee中出现在“?>”的结尾是无法去除注释的问题,大家试一试:

<?php
function replace_php_src($src) {
static $IW = array(
T_CONCAT_EQUAL, // .=
T_DOUBLE_ARROW, // =>
T_BOOLEAN_AND, // &&
T_BOOLEAN_OR, // ||
T_IS_EQUAL, // ==
T_IS_NOT_EQUAL, // != or <>
T_IS_SMALLER_OR_EQUAL, // <=
T_IS_GREATER_OR_EQUAL, // >=
T_INC, // ++
T_DEC, // --
T_PLUS_EQUAL, // +=
T_MINUS_EQUAL, // -=
T_MUL_EQUAL, // *=
T_DIV_EQUAL, // /=
T_IS_IDENTICAL, // ===
T_IS_NOT_IDENTICAL, // !==
T_DOUBLE_COLON, // ::
T_PAAMAYIM_NEKUDOTAYIM, // ::
T_OBJECT_OPERATOR, // ->
T_DOLLAR_OPEN_CURLY_BRACES, // ${
T_AND_EQUAL, // &=
T_MOD_EQUAL, // %=
T_XOR_EQUAL, // ^=
T_OR_EQUAL, // |=
T_SL, // <<
T_SR, // >>
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
);
if(is_file($src)) {
if(!$src = file_get_contents($src)) {
return false;
}
}
$tokens = token_get_all(substr ($src, 0, -2));

$new = "";
$c = sizeof($tokens);
$iw = false; // ignore whitespace
$ih = false; // in HEREDOC
$ls = ""; // last sign
$ot = null; // open tag
for($i = 0; $i < $c; $i++) {
$token = $tokens[$i];
if(is_array($token)) {
list($tn, $ts) = $token; // tokens: number, string, line
$tname = token_name($tn);
if($tn == T_INLINE_HTML) {
$new .= $ts;
$iw = false;
} else {
if($tn == T_OPEN_TAG) {
if(strpos($ts, " ") || strpos($ts, "n") || strpos($ts, "t") || strpos($ts, "r")) {
$ts = rtrim($ts);
}
$ts .= " ";
$new .= $ts;
$ot = T_OPEN_TAG;
$iw = true;
} elseif($tn == T_OPEN_TAG_WITH_ECHO) {
$new .= $ts;
$ot = T_OPEN_TAG_WITH_ECHO;
$iw = true;
} elseif($tn == T_CLOSE_TAG) {
if($ot == T_OPEN_TAG_WITH_ECHO) {
$new = rtrim($new, "; ");
} else {
$ts = " ".$ts;
}
$new .= $ts;
$ot = null;
$iw = false;
} elseif(in_array($tn, $IW)) {
$new .= $ts;
$iw = true;
} elseif($tn == T_CONSTANT_ENCAPSED_STRING
|| $tn == T_ENCAPSED_AND_WHITESPACE)
{
if($ts[0] == '"') {
$ts = addcslashes($ts, "ntr");
}
$new .= $ts;
$iw = true;
} elseif($tn == T_WHITESPACE) {
$nt = @$tokens[$i+1];
if(!$iw && (!is_string($nt) || $nt == '$') && !in_array($nt[0], $IW)) {
$new .= " ";
}
$iw = false;
} elseif($tn == T_START_HEREDOC) {
$new .= "<<<Sn";
$iw = false;
$ih = true; // in HEREDOC
} elseif($tn == T_END_HEREDOC) {
$new .= "S;";
$iw = true;
$ih = false; // in HEREDOC
for($j = $i+1; $j < $c; $j++) {
if(is_string($tokens[$j]) && $tokens[$j] == ";") {
$i = $j;
break;
} else if($tokens[$j][0] == T_CLOSE_TAG) {
break;
}
}
} elseif($tn == T_COMMENT || $tn == T_DOC_COMMENT) {
$iw = true;
} else {
if(!$ih) {
$ts = strtolower($ts);
}
$new .= $ts;
$iw = false;
}
}
$ls = "";
} else {
if(($token != ";" && $token != ":") || $ls != $token) {
$new .= $token;
$ls = $token;
}
$iw = true;
}
}
return $new.' ?>';
}

//大家可以这样测试一下
file_put_contents('test1.php',replace_php_src('test.php'));

偏爱自由 2017-05-03 13:58:37

看看这个:Strip comments with the tokenizer

 <?php
/*
* T_ML_COMMENT does not exist in PHP 5.
* The following three lines define it in order to
* preserve backwards compatibility.
*
* The next two lines define the PHP 5 only T_DOC_COMMENT,
* which we will mask as T_ML_COMMENT for PHP 4.
*/
if (!defined('T_ML_COMMENT')) {
define('T_ML_COMMENT', T_COMMENT);
} else {
define('T_DOC_COMMENT', T_ML_COMMENT);
}

$source = file_get_contents('example.php');
$tokens = token_get_all($source);

foreach ($tokens as $token) {
if (is_string($token)) {
// simple 1-character token
echo $token;
} else {
// token array
list($id, $text) = $token;

switch ($id) {
case T_COMMENT:
case T_ML_COMMENT: // we've defined this
case T_DOC_COMMENT: // and this
// no action on comments
break;

default:
// anything else -> output "as is"
echo $text;
break;
}
}
}
?>

归属感 2017-04-07 02:59:13

这个以前我收集一位高人写的,但是忘了地址,请谅解,后补上(^_^)

<?php
/*
* PHP去源码注释及轻度压缩数据
*/
class REMOVE_COMMENT{
var $path = "";
var $file = "";
var $content = "";
var $after_content = "";
var $compact_content = "";
var $reg_comment = "!((/*)[sS]*?(*/))|(//.*)!";
var $reg_space = "![ ]+!";
var $reg_all_space = "!s+!";
var $file_info = array();
var $alowed_type = array("php","css","c","c++","txt","html","htm","tpl");
var $save_file = "";
function __construct($path="") {
$this->load_file($path);
}
function load_file($path=""){
global $error;
$this->free();
if(!$path){
$this->halt($error[0]);
}elseif(!file_exists($path)){
$this->halt($error[1]);
}else{
$this->path = $path;
$this->open_file();
}
}
//open file if exist
protected function open_file(){
global $error;
if(!is_file($this->path)){
$this->halt($error[2]);
}else{
if(!is_readable($this->path)){
$this->halt($error[3]);
}else{
if(!($this->file = @fopen($this->path, "r"))){
$this->halt($error[4]);
}else{
$this->file_info = pathinfo($this->path);
if(in_array($this->file_info["extension"], $this->alowed_type)){
$this->read_file();
}else{
$this->halt($error[2]);
}
}
}
}
}
//read the file.
protected function read_file(){
if(!$this->file){
$this->open_file();
}
if($this->file){
$this->content = file_get_contents($this->path);
}
}
//remove all comments
function remove(){
if(!$this->content){
$this->read_file();
}
$this->after_content = preg_replace($this->reg_comment, "", $this->content);
return $this->content;
}
//compact the datas, remove superabundant space,datas easy to read.
function compact(){
if(!$this->after_content){
$this->remove();
}
$this->compact_content = preg_replace($this->reg_space, " ", $this->after_content);
return $this->compact_content;
}
//compact the datas, datas hard to read.
function compact_hightly(){
if(!$this->after_content){
$this->remove();
}
$this->compact_content = preg_replace($this->reg_all_space, " ", $this->after_content);
return $this->compact_content;
}
//write the data into a file
function save($file_name=""){
global $error;
$tmp_name = date("Y-m-dHms").".".$this->file_info["extension"];
$handle = "";
if($file_name){
if(!@$handle = fopen($file_name, "r")){
$this->halt($error[5]);
if(!@$handle = fopen($file_name, "w")){
$this->halt($error[6]);
return 0;
}else{
@fclose($handle);
$this->halt($error[7]);
}
}else{
@fclose($handle);
}
}else{
$file_name = $tmp_name;
}
if($this->compact_content){
$content = $this->compact_content;
}elseif($this->after_content){
$content = $this->after_content;
}
if(!$content){
$this->halt($error[8]);
return 0;
}
if(@file_put_contents($file_name,$content)){
$this->halt("success!save as file:".realpath($file_name));
}
return 1;
}
//display error message
protected function halt($error_msg){
print printf("<h2>error : %s</h2>n",$error_msg);
}
//free occupied memery
function free(){
unset($this->after_content);
unset($this->compact_content);
unset($this->content);
unset($this->file);
unset($this->path);
}
}
/*//使用示例
header("Content-type:text/html;charset:utf-8");
$d = new REMOVE_COMMENT(__FILE__);
echo htmlentities($d->compact());
$d->save("example.php");
*/
?>

清晨说ぺ晚安 2017-03-22 21:45:18

我给写了一个,正则就能搞定呀,可能不是最快的,但是我认为是最简单的
支持 "/** */"、"//"、"#" 风格的注释

 <?php
$fileName = "tt.php";
$file =file_get_contents($fileName);
$file = preg_replace('/((/*[sS]*?*/)|(//.*)|(#.*))/', '', $file);
file_put_contents($fileName, $file);
?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文