php常规注释删除

发布于 2024-12-02 00:11:22 字数 296 浏览 0 评论 0原文

如何删除多行注释? ( /* comment php */) 来自文件 .php 如何删除单个评论? ( // comment ) 来自 file.php 如何删除输入结束行? 样本 : 单个注释行

$G["url"] = "http://".$_SERVER["HTTP_HOST"]

multi comment line

      /*
         * 310 
         * - "::"
         * - $col
         */

how remove multi line comment ? ( /* comment php */) from file .php
how remove single comment ? ( // coment ) from file.php
how remove enter end line ?
sample :
single comment line

$G["url"] = "http://".$_SERVER["HTTP_HOST"]

multi comment line

      /*
         * 310 
         * - "::"
         * - $col
         */

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

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

发布评论

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

评论(3

喜爱皱眉﹌ 2024-12-09 00:11:22

尝试使用下面发布的代码,它甚至会忽略“ /* comment2 */ ”等字符串上的注释标记,

<?php

$fileIn = "src.php"; 
$fileOut = "srcCompressed.php"; 

$text = file_get_contents($fileIn);

$text = compress_php_src($text);

file_put_contents($fileOut, $text);


function compress_php_src($src) {
    // Whitespaces left and right from this signs can be ignored
    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($src);

    $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, "\n\t\r");
                    }
                    $new .= $ts;
                    $iw = true;
                } elseif($tn == T_WHITESPACE) {
                    $nt = @$tokens[$i+1];
                    if(!$iw && (!is_string($nt) || $nt == '

归功于 gelamu 函数 compress_php_src()

) && !in_array($nt[0], $IW)) { $new .= " "; } $iw = false; } elseif($tn == T_START_HEREDOC) { $new .= "<<<S\n"; $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; } ?>

归功于 gelamu 函数 compress_php_src()

Try using the code posted below, it even ignores comment tokens on strings like " /* comment2 */ "

<?php

$fileIn = "src.php"; 
$fileOut = "srcCompressed.php"; 

$text = file_get_contents($fileIn);

$text = compress_php_src($text);

file_put_contents($fileOut, $text);


function compress_php_src($src) {
    // Whitespaces left and right from this signs can be ignored
    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($src);

    $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, "\n\t\r");
                    }
                    $new .= $ts;
                    $iw = true;
                } elseif($tn == T_WHITESPACE) {
                    $nt = @$tokens[$i+1];
                    if(!$iw && (!is_string($nt) || $nt == '

Credits to gelamu function compress_php_src().

) && !in_array($nt[0], $IW)) { $new .= " "; } $iw = false; } elseif($tn == T_START_HEREDOC) { $new .= "<<<S\n"; $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; } ?>

Credits to gelamu function compress_php_src().

无声静候 2024-12-09 00:11:22

没有理由从代码中删除注释。处理它们的开销非常小,以至于不值得付出这样的努力。

There's no reason to remove comments from your code. The overhead from processing them is so incredibly minimal that it doesn't justify the effort.

許願樹丅啲祈禱 2024-12-09 00:11:22

我想您可能正在寻找 php_strip_whitespace()

I think you are probably looking for php_strip_whitespace().

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