浮叶蓝空的博客
分享学习心得,记录学习笔记
文章:
84
访问:
526142
登录
关于
首页
今天是:
2025年09月09日 星期二
类别
PHP(39)
其他笔记(20)
Java(1)
C++(17)
mysql(2)
JavaScript(4)
近期文章
Linux下Cmake引用第三方静态库示例
Linux下使用Cmake构建静态库示例
Ubuntu设置自动挂载硬盘,挂载U盘
压力测试工具Apache JMeter基本使用
使用PHP脚本借助FFmpeg一键合并视频
Shell中的>、1>、2>、2>&1、&>详解
PHP开启多进程实现异步非阻塞并行执行任务
博主推荐
PHP实现文件下载接口
Qt读写注册表,C++读写注册表
PHP动态修改配置文件,存储为文件
MySql常用语法
Powershell远程连接
PHP从字符串中获取需要的内容部分
封装PHP的HTTP请求
指定项目目录,提取代码
fuyelk
2019年11月25日
2153
#### 指定项目目录、需要的代码文件扩展名、保存代码的文件路径三项参数,即可将项目中的所有满足的代码提取到一个文件中。 ##### 通常用于申请软著的代码提取。也可以在第四个参数中设置需要保留的总代码行数(实际保留行数可能大于设定行数,取决于行数够了的时候最后一个读取的文件还没有结束,那么这个文件会全部保留) ## 运行截图 [](https://www.milinger.com/a169.html "PHP脚本提取代码,提取软著代码") ```php <?php // +------------------------------------------- // | 指定项目路径,提取全部代码到指定文件中 // +------------------------------------------- // | Author: fuyelk <fuyelk@fuyelk.com> // +------------------------------------------- // | Date: 2019/11/25 // +------------------------------------------- // | Update Time: 2022/11/11 16:42 // +------------------------------------------- /** * 抛出错误 * @param string $msg */ function throwError(string $msg) { $error_html = <<<EOF <!doctypehtml><html lang=en><meta charset=UTF-8><title>出错了!</title><style>.title{margin:30px auto;text-align:center}.title button{height:30px;line-height:30px;width:100px;background:#19c6a6;border:0;border-radius:5px;margin:10px auto;padding:0 5px;cursor:pointer;color:#fff}.title #clock{margin-left:5px}.title button:hover{background:#19a383}</style><div class=title><h1>$msg</h1><button id=goBack type=button>点击返回<span id=clock></span></button></div><script>let d=3; let c=document.getElementById("clock");c.innerText=d; let b=setInterval(function(){c.innerText=--d;if(d<=0){clearInterval(b);window.location.href="javascript:history.go(-1)"}},1000);document.getElementById("goBack").onclick=function(){clearInterval(b);window.location.href="javascript:history.go(-1)"};</script> EOF; print_r($error_html); die(); } session_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 参数校验 if (empty($_POST['extension']) || empty($_POST['project']) || empty($_POST['output'])) { throwError('请填写每一项内容'); } set_time_limit(0); // 永不超时 ignore_user_abort(true); // 浏览器关闭,程序不终止 $project_path = $_POST['project']; $output = $_POST['output']; $total_rows = $_POST['total_rows'] ?: 0; $extension = trim($_POST['extension'], ','); $extension = strpos($extension, ',') ? explode(',', $extension) : [$extension]; $_SESSION['extract_code_post'] = $_POST; new ExtractCode($project_path, $extension, $output, $total_rows); } else { $default = $_SESSION['extract_code_post']; if (empty($default)) { $default = ['project' => '', 'output' => '', 'total_rows' => '', 'extension' => '']; } $html = <<<EOF <!doctypehtml><html lang=en><meta charset=UTF-8><title>合并代码</title><style>.title{margin:30px auto;text-align:center}.content{width:600px;height:322px;margin:20px auto auto auto;background-color:#eee}.div-form{width:560px;height:280px;background-color:#fff;border-radius:10px;border:dotted 1px;padding:20px 20px}.input-item{width:500px;height:50px;margin-top:10px}.input-title{width:110px;height:50px;line-height:50px;display:inline-block;text-align:right}.input-item input{width:370px;height:30px;border:dotted 1px;border-radius:5px;padding:0 5px;outline:0}.div-submit{width:560px;height:50px;text-align:center}.div-submit button{height:30px;line-height:30px;width:80px;background:#19c6a6;border:0;border-radius:5px;margin:10px auto;cursor:pointer;color:#fff}.div-submit button:hover{background:#19a383}</style><div class=title><h1>提取项目代码到指定文件中</h1></div><div class=content><div class=div-form><form action="" method=post><div class=input-item><label class=input-title for=inp-project>项目路径:</label><input autocomplete=off id=inp-project name=project placeholder=如:D:\app\program value="{$default['project']}"></div><div class=input-item><label class=input-title for=inp-extension>文件扩展名:</label><input autocomplete=off id=inp-extension name=extension placeholder=如:h,cpp,php(多个以英文逗号分隔) value="{$default['extension']}"></div><div class=input-item><label class=input-title for=inp-output>保存文件:</label><input autocomplete=off id=inp-output name=output placeholder=如:D:\code\allcode.txt value="{$default['output']}"></div><div class=input-item><label class=input-title for=inp-total_rows>代码行数:</label><input autocomplete=off id=inp-total_rows name=total_rows placeholder=不限制 value="{$default['total_rows']}"></div><div class=div-submit><button>提交</button></div></form></div></div> EOF; echo $html; } /** * 提取项目代码 * @author fuyelk <fuyelk@fuyelk.com> */ class ExtractCode { /** * @var array 需要的文件扩展名 */ private $extension; /** * @var string 输出文件名 */ private $output; /** * @var int 总行数 */ private $totalRows; /** * @var string 完成后返回的内容 */ private $finished_html = <<<EOF <!doctypehtml><html lang=en><meta charset=UTF-8><title>合并代码完成</title><style>.title{margin:30px auto;text-align:center}.title button{height:30px;line-height:30px;width:100px;background:#19c6a6;border:0;border-radius:5px;margin:10px auto;padding:0 5px;cursor:pointer;color:#fff}.title #clock{margin-left:5px}.title button:hover{background:#19a383}</style><div class=title><h1>提取完成</h1><button id=goBack type=button>点击返回<span id=clock></span></button></div><script>let d=3; let c=document.getElementById("clock");c.innerText=d; let b=setInterval(function(){c.innerText=--d;if(d<=0){clearInterval(b);window.location.href="javascript:history.go(-1)"}},1000);document.getElementById("goBack").onclick=function(){clearInterval(b);window.location.href="javascript:history.go(-1)"};</script> EOF; /** * @param string $project_path 项目路径 * @param array $extension 文件扩展名集合 * @param string $output 输出文件路径 * @param int $total_rows 总代码行数 */ public function __construct(string $project_path, array $extension, string $output, int $total_rows) { $this->extension = $extension; $this->output = $output; $this->totalRows = $total_rows; if (is_file($output)) { @unlink($output); } $this->readCodeForEach($project_path); echo $this->finished_html; } /** * 提取代码 * @param string $project_path 文件路径 */ private function readCodeForEach(string $project_path) { if (!is_dir($project_path)) { throwError('項目路径有误'); } $handle = opendir($project_path); if (!$handle) { throwError('读取项目目录失败'); } while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { // 当前项目是文件夹则进入下一层 if (is_dir($project_path . '/' . $file)) { $Path = $project_path . '/' . $file; $this->readCodeForEach($Path); continue; } // 当前项目扩展名是需要的,则读取并保存 if (in_array(pathinfo($file, PATHINFO_EXTENSION), $this->extension)) { $current_data = file_get_contents($project_path . '/' . $file); // 检查行数是否够了 $this->isEnough(); $fp = fopen($this->output, "a"); fwrite($fp, "\n" . $current_data); fclose($fp); } } } closedir($handle); } /** * 判断代码行数是否足够了 * @return void * @author fuyelk <fuyelk@fuyelk.com> */ private function isEnough() { // 未生成文件或为设定行数,则继续 if (!is_file($this->output) || !$this->totalRows) { return; } $count_line = 0; if ($fp = fopen($this->output, 'r')) { // 判断是否到了文件底部 while (!feof($fp)) { if (fgets($fp)) $count_line++; if ($count_line > $this->totalRows) { fclose($fp); print_r($this->finished_html); die(); } } fclose($fp); } } } ```
上一篇:
Thinkphp Hook 钩子
下一篇:
WEB打包为桌面程序
1人点赞
登录后评论
友情链接
doywb
2018-2025 Copyright© 米灵尔 浮叶蓝空
豫ICP备15007436号-1
豫公网安备 41152302000146号