教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 替换目录下文件指定内容

php 替换目录下文件指定内容

发布时间:2017-10-25   编辑:jiaochengji.com
教程集为您提供php 替换目录下文件指定内容等资源,欢迎您收藏本站,我们将为您提供最新的php 替换目录下文件指定内容资源
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7890')"><textarea id="copy7890" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7890>

/****************************
    * 获取目录下的所有文件
    * [$dir] 文件夹路径
    ****************************/
    function deepScanDir($dir) {
        $fileArr = array ();
        $dirArr = array ();
        $dir = rtrim($dir, '//');
        if (is_dir($dir)) {
            $dirHandle = opendir($dir);
            while (false !== ($fileName = readdir($dirHandle))) {
                $subFile = $dir . DIRECTORY_SEPARATOR . $fileName;
                if (is_file($subFile)) {
                    $fileArr[] = $subFile;
                }
                elseif (is_dir($subFile) && str_replace('.', '', $fileName) != '') {
                    $dirArr[] = $subFile;
                    $arr = $this->deepScanDir($subFile);
                    $dirArr = array_merge($dirArr, $arr['dir']);
                    $fileArr = array_merge($fileArr, $arr['file']);
                }
            }
            closedir($dirHandle);
        }
        return array (
            'dir' => $dirArr,
            'file' => $fileArr
        );
    }

 

/*
    * 替换成APP中可用的路径,在web文件夹中
    */
    public function ok_web(){
        //查找字符
        $yuanlai = array(
            '"/resources/',
            '"/uploads/',
            '"/web/',
            'href="/"',
            '/web',
            'typedir \'/\' v.aid "',
            'v.litpic',
        );
        //替换字符
        $tihuan = array(
            '"../resources/',
            '"../uploads/',
            '"',
            'href="../index.html"',
            '',
            'v.aid "',
            '".." v.litpic'
        );
        
        //查找的文件夹
        $dir = WEBROOT.'/app/web';
        //获取文件
        $dirs = $this->deepScanDir($dir);
        //文件字符串替换
        foreach($dirs['file'] as $file){
            $file = 'G:\hospital\hospital\admin/app/web\yiyuanzhuanjia.html';
            $txt = file_get_contents($file);
            $txt =  str_replace($yuanlai,$tihuan,$txt);
            file_put_contents($file,$txt);echo $txt;exit;
        }
      
    }

</td></tr></table>

您可能感兴趣的文章:
php无限遍历目录代码
php 静态页生成的二种方法解析
php在一个文件不断写入字符串
多规则替换过滤nginx模块nginx_substitutions_filter
动态网页制作技术PHP的十个应用技巧
python怎么替换字符串的内容
php 字符串替换函数
批量替换当前目录后缀名的批处理脚本
php怎么改成html
php命令行下相对路径问题的解决方法

[关闭]
~ ~