2025-05-13 18:47:25 +08:00

61 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
//本脚本为将特定目录下的所有源码文件去掉空格、空行并在当前脚本所在位置生成一个docx文件里面是所有源码字符串的集合用来提供申请软著的源码文件。
//源码文件路径(可多个)
$paths = [
'F:\code_project\back_end\php\git_depository\new-diverse-youth-night-school-backend\NewDiverseYouthNightSchool\application\api\controller\school\newactivity',
'F:\code_project\back_end\php\git_depository\new-diverse-youth-night-school-backend\NewDiverseYouthNightSchool\application\api\controller\school\newworker',
'F:\code_project\back_end\php\git_depository\new-diverse-youth-night-school-backend\NewDiverseYouthNightSchool\application\api\controller\school\NewActivity.php',
];
//输出文件路径
$out_path = '.\out.docx';
//源码文件后缀数组
$exts = ["php","js","html"];
//过滤的正则
//去掉空格、空行
//$filter = '/\s(?=\s)/';
//保留原格式,包括其他行的回车换行符,只去除无内容的行
$filter = '/\s(?!\S)/';
//遍历目录下所有文件(递归函数遍历文件数,不漏掉深层文件)
function get_all_files($path)
{
$files = array();
if (is_dir($path)) {
$fh = opendir($path);
while (false !== ($file = readdir($fh))) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
$files = array_merge($files, get_all_files($path . '/' . $file));
} else {
$files[] = $path . '/' . $file;
}
}
}
}
return $files;
}
//源码字符串
$str = '';
$files = [];
foreach ($paths as $path) {
$files = array_merge($files, get_all_files($path));
}
//遍历目录下所有文件
foreach ($files as $file) {
//判断文件后缀是否在数组中
if (in_array(substr(strrchr($file, '.'), 1), $exts)) {
//得到文件内容
$file_content = file_get_contents($file);
//去掉空格、空行
$file_content = preg_replace($filter, '', $file_content);
$str .= $file_content;
}
}
//写入文件
if($str)file_put_contents($out_path, $str);