thinkphp3.2.3 批量包含文件
自己瞎写的...凑合看吧...核心就是用正则 表达式 或者 字符串 str_replace 进行 替换.......
/**
* 批量包含---,不能递归包含!!! 请不要在目标目录 包含 文件夹,因为没有做处理----
* @author ******
* @param string $srPath 例如 "file="adc/*""
* @param string $oldStr 旧的字符串
* @param string $oldContent 旧的内容
* @return string
*/
private function parseMulti($srPath, $oldStr, $oldContent): string
{
$content = '';
$filePath = str_replace('/*', '', str_replace('"', '', explode('file=', $srPath)[1]));
//内部可递归方法..将子文件夹里的文件也包含到文件中去...
$fileArr = [];
$list_file = function ($dir, $pDir = '') use (&$list_file, &$fileArr) {
$list = scandir($dir); // 得到该文件下的所有文件和文件夹
foreach ($list as $file) {//遍历
$file_location = $dir . "/" . $file;//生成路径
if ($file != "." && $file != "..") {
if (is_dir($file_location)) { //判断是不是文件夹
$list_file($file_location, $file); //继续遍历
} else {
if ($pDir != '') {
$file = $pDir . '/' . $file;
}
array_push($fileArr, $file);
}
}
}
};
$list_file(MODULE_PATH . 'View/' . $filePath);
foreach ($fileArr as $filename) {
if ($filename !== '.' && $filename !== '..') {
$newPath = str_replace('*', explode('.', $filename)[0], $srPath);
$array = $this->parseXmlAttrs($newPath);
$file = $array['file'];
$content .= $this->parseIncludeItem($file, $array, true);
}
} //压缩 html....
$page_html = str_replace("\r\n", '', $content); //清除换行符
$page_html = str_replace("\n", '', $page_html); //清除换行符
$page_html = str_replace("\t", '', $page_html); //清除制表符
$pattern = array(
"/> *([^ ]*) *</", //去掉注释标记
"/[\s]+/",
"/<!--[^!]*-->/",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'",
"'/\*{1,2}[\s\S]*?\*/'", );
$replace = array(">\\1<", " ", "", "\"", "\"", "", "");
$content = preg_replace($pattern, $replace, $page_html); $content = str_replace($oldStr, $content, $oldContent); return $content;
}
在 Template.class.php 中 加入方法--
// 解析模板中的include标签
protected function parseInclude($content, $extend = true)
{
// 解析继承
if ($extend)
$content = $this->parseExtend($content);
// 解析布局
$content = $this->parseLayout($content);
// 读取模板中的include标签
$find = preg_match_all('/' . $this->config['taglib_begin'] . 'include\s(.+?)\s*?\/' . $this->config['taglib_end'] . '/is', $content, $matches); //有 * 号.代表包含目录下所有!!! 修改tp,包含所有时,不要带 / if ($find) {
for ($i = 0; $i < $find; $i++) {
$include = $matches[1][$i];
$array = $this->parseXmlAttrs($include);
$file = $array['file'];
unset($array['file']);
if ($include[strlen($include) - 2] === '*') { //加一个判断...如果是以* 结尾...则 进行 批量操作 例如 <include flie="abc/*"/>
$content = $this->parseMulti($include, $matches[0][$i], $content);
} else {
$content = str_replace($matches[0][$i], $this->parseIncludeItem($file, $array, $extend), $content);
}
} }
return $content;
}
thinkphp3.2.3 批量包含文件的更多相关文章
- gulp批量打包文件并提取公共文件
gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器. browseriyf是模块化打包工具. 一般情况下,Browserify 会把所有的模块打包成单个文件.单个文件在大多数情况下是 ...
- 用C#Winform写个简单的批量清空文件内容和删除文件的小工具
用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...
- 用SSH指令批量修改文件夹 文件权限和拥有者
在linux系统下或登录ssh可以批量修改文件权限 wwwroot目录下的所有目录的权限递归设置为755 cd wwwrootfind -type d -exec chmod 755 {} \;或者是 ...
- Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例
本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...
- python批量拷贝文件
普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...
- R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数
一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...
- 关于windows下自带的forfile批量删除文件bat命令
最近在开发的过程中,为了节省资源,需要用到windows下批量删除文件的批处理命令,也就是bat 主要内容: forfiles /p "E:\pictures" /m * /d - ...
- 【Python】简单的脚本,轻松批量修改文件名称
使用python脚本,批量修改文件夹名称 先创建一些没用的案例文件 import os #创建新文件夹 dir = os.makedirs('D:\\SomeThing\\testfile') #将文 ...
- python批量进行文件修改操作
python批量修改文件扩展名 在网上下载了一些文件,因为某种原因,扩展名多了一个后缀'.xxx',手动修改的话因为文件太多,改起来费时费力,于是决定写个小脚本进行修改. 1.要点: import r ...
随机推荐
- Android4.0+锁屏程序开发——设置锁屏页面篇
[如何开发一个锁屏应用] 想要开发一个锁屏应用,似乎很难,其实并没有想象中那么难. 从本质上来说,锁屏界面也只是一个Activity而已,只是这个界面比较特殊,在我们点亮屏幕的时候,这个界面就会出现. ...
- 客户端级别的渲染分析工具 dynaTrace
dynaTrace Ajax Edition是一款很好的javaScript性能分析工具.目前支持IE和Firefox 2款浏览器. dynaTrace如图所示: 点击Click here to st ...
- hadoop搭建好,启动服务后,无法从web界面访问50070
在hadoop完全分布式搭建好以后,从主节点启动正常,使用jps查看启动的进程,正常,在几个从节点上使用jps查看,显示正常,但从web上输入下面网址: http://主节点IP:50070 无法正常 ...
- Solidity mapping循环
https://medium.com/@blockchain101/looping-in-solidity-32c621e05c22
- Hyperledger Fabric1.0 整体结构
整体结构 Hyperledger Fabric 在 1.0 中,架构已经解耦为三部分: fabric-peer:主要起到 peer 作用,包括 endorser.committer 两种角色: fab ...
- 类操作,removeClass&addClass
// 添加类 function addClass(node,className){ var reg=new RegExp("\\b"+classNa ...
- Controlling Session Behavior in Asp.Net MVC4
Posted By : Shailendra Chauhan, 06 Jan 2013 Updated On : 11 Jun 2014 By default, Asp.Net MVC support ...
- GlobalAlloc()和malloc()、HeapAlloc()
两者都是在堆上分配内存区. malloc()是C运行库中的动态内存分配函数,WINDOWS程序基本不使用了,因为它比WINDOWS内存分配函数少了一些特性,如,整理内存. GlobalAlloc( ...
- Stomp与Jackson
读取Stomp消息的类在StompDecoder中,org.springframework.messaging.simp.stomp.StompDecoder. Jackson把json转对象是在如下 ...
- 系统架构一:snmp+mrtg服务器监控
//@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备 ...