PHP 去除文件内的注释并生成新的文件夹及文件

此方法是用于清除文件的注释

  • 第一步,创建去除php代码注释的方法,上代码
 /**

     * 去除PHP代码注释

     * @param  string $content 代码内容

     * @return string 去除注释之后的内容

     */
    public function removeComment($content){
        // (\/\*.*\*\/)|(#.*?\n)|(\/\/.*?\n)|
        //这里使用<!-- -->注释为例,其他注释可正则替换
        return preg_replace("/(<!--[\w\W\r\n]*?-->)/s", '', str_replace(array("\r\n", "\r"), "\n", $content));

    }

  • 第二步,创建根据路径创建文件的方法,上代码
//判断路径文件是否存在 不存在则创建 dirname是只当前文件路径上一级
public function createDir($path){
        if (!file_exists($path)){
            $this->createDir(dirname($path));
            mkdir($path, 0777);
        }
    }
  • 第三步,创建方法并传入路径,根绝路径打开文件进行注释消除及创建新的文件,上代码:
//去除文件的注释

    public function drop_xx(){

        // echo "源码:";

        // show_source('./application/themes/pc/views/posts/post_v2.php');

        // echo "去除注释后:";
        //新的文件路径
        $path = './application/themes/pc2/views/posts/post_v2.php';
	//判断新的文件是否存在  不存在则创建
        if(!is_dir($path)){
            $this->createDir(dirname($path));
        }
        //消除文件中注释<!-- -->
        $content = $this->removeComment(file_get_contents('./application/themes/pc/views/posts/post_v2.php'));
        //写入新文件
        file_put_contents($path,$content);
        if (file_exists($path))
         {echo "ok";}   
        else
         {echo "ng";}

    }

小问题记录,喜欢或有用的,点赞、收藏+关注,蟹蟹您


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,可以使用以下步骤进行批量遍历多个文件夹文件注释: 1. 使用递归方式遍历文件夹内的所有文件。 2. 对于每个文件,判断其文件类型是否为C或C++文件,可以使用文件后缀名进行判断。 3. 对于C或C++文件,使用文件输入输出流读取文件内容,将需要注释的代码行进行注释处理,可以使用正则表达式或字符串处理方式进行注释。 4. 使用文件输入输出流将处理后的文件内容写入原文件或写入文件中。 下面是一个C++示例代码,可以实现上述功能: ```cpp #include <iostream> #include <fstream> #include <regex> #include <filesystem> using namespace std; namespace fs = std::filesystem; void commentFile(const string& filename) { string line; string content; ifstream inputFile(filename); if (!inputFile.is_open()) { cout << "Failed to open file: " << filename << endl; return; } regex pattern("\\s*(//|/\\*|\\*/|#).*"); while (getline(inputFile, line)) { if (regex_match(line, pattern)) { content += line + "\n"; } else { content += "// " + line + "\n"; } } inputFile.close(); ofstream outputFile(filename); if (!outputFile.is_open()) { cout << "Failed to open file: " << filename << endl; return; } outputFile << content; outputFile.close(); } void commentFolder(const string& path) { for (const auto& entry : fs::recursive_directory_iterator(path)) { if (entry.is_regular_file()) { const string& filename = entry.path().string(); if (filename.substr(filename.find_last_of(".") + 1) == "c" || filename.substr(filename.find_last_of(".") + 1) == "cpp") { commentFile(filename); } } } } int main() { commentFolder("C:/MyProject"); return 0; } ``` 这段代码遍历指定文件夹内的所有C和C++文件,并对文件内容进行注释处理,处理后的文件内容将覆盖原文件。您可以根据需要进行修改,比如将处理后的文件写入文件中,而不是覆盖原文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值