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("
", '', $content); //清除换行符
        $page_html = str_replace("
", '', $page_html); //清除换行符
        $page_html = str_replace("	", '', $page_html); //清除制表符
        $pattern = array(
            "/> *([^ ]*) *</", //去掉注释标记
            "/[s]+/",
            "/<!--[^!]*-->/",
            "/" /",
            "/ "/",
            "'/*[^*]**/'",
            "'/*{1,2}[sS]*?*/'",

        );
        $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'] . 'includes(.+?)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;
    }
原文地址:https://www.cnblogs.com/whm-blog/p/8986947.html