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 ...
随机推荐
- laravel中的模型关联之(一对一)
一对一 一对一的关联关系就相当于,赞和文章之间的关系,一个用户只能赞一次一个文章,只能和文章发生一次关系, 用户和赞的文章是唯一的一份,这里举得是判断用户是否赞过某个文章 一个参数都是你要获取的模型, ...
- sql语句查询中exists中为什么要用select 1?
select * from call_cdr_xz_200609 a where and a.ori_charge<>0 and exists(select 1 from special ...
- 我使用的网址--Hadoop
1.Hadoop 官网下载:http://hadoop.apache.org/releases.html 各版本网址:http://mirror.bit.edu.cn/apache/hadoop/co ...
- ROS indigo 删除和安装
删除比较容易: sudo apt-get remove ros-jade-desktop-full 但是如果怕删不干净可以采用: sudo apt-get remove ros-* ,但是不确定会 ...
- Java生产者消费者问题
1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...
- Smarty3——从配置文件获取的变量
再使用配置变量前要 引入配置变量即:{$config_load file=‘file_path’}$marty3中可以从配置文件中 用 # 号包起来引用配置文件中的变量({#config_var_na ...
- GO程序设计2——面向过程基础知识
1 简介 GO语言google开发的新语言.有如下特性: 自动垃圾回收.更丰富的内置数据类型.函数多返回值.错误处理.匿名函数和闭包.类型和接口.并发编程.反射.多语言混合编程 package mai ...
- hdu 2211 杀人游戏
设f(N,K)返回最后取出的编号 那么f(n,k)进行第一次选后,剩下n-n/k个人,这剩下的人里最后被取出的编号为f(n-n/k,k)记为x 那么它在前一次队列里的编号则是(x-1)/(k-1)+x ...
- maven构建非法字符解决办法
CI使用maven做版本构建时候碰到了一个问题,有个java源码始终编译报错,错误发生在文件第一行. 出错内容是: ***.java:[1,1] 非法字符: \65279 后面上网看了,原来是文件编码 ...
- wpf path语法
http://www.cnblogs.com/HQFZ/p/4452548.html WPF系列 Path表示语法详解(Path之Data属性语法)