php正则替换 某些字符串,html替换某些标签,Thinkphp3升级Thinkphp5替换模版标签

 虽然Thinkphp5可以更改HTML模版标签,的前后字符,

 但是又没搜到相关的正则替换某些字符串,所以就看了下正则相关文档,分享给大家吧!

  

<?php  
/*/Public/Home/default/re.php
文本替换 小工具
QQ496631085
XiaoHe
*/
    
    //打印变量标签好调试
    function p($data){
        if(is_array($data)){
            var_dump($data);
        }else{
            echo '<br>'.$data.'<br>';
        }
    }

    //分割后缀
    function getExt2($filename){
       $ext = strrchr($filename,'.');
       return $ext;
    }
    

    function re_tihuan($val){
    /*
    preg_replace 正则替换
    strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签。
    */

        // <block name="page-style">
        $newval=preg_replace('/(\<)(block .*?)(\>)/','{$2}',$val);
        // 替换结果{block name="page-style"}
    
        
        
        // <extend name="System:base"/>
        $newval=preg_replace('/(\<)(extend .*?)(\:)(.*?)(\>)/','{$2_$4}',$newval);
        // 替换结果 {extend name="System_base"/}


        // <volist name="data.list" id="vo">
        $newval=preg_replace('/(\<)(volist.*?)(\>)/','{$2}',$newval);
        // 替换结果{volist name="data.list" id="vo"}


        // <empty name="data.list">
        $newval=preg_replace('/(\<)(empty.*?)(\>)/','{$2}',$newval);
        //替换结果 {empty name="data.list"}
        return $newval;
    }
    
    function tihuan1($val)
    {//替换内容
        $list=[
        //     '<block name="page-style">'=>'{block name="page-style"}',
        // '<block name="page-style-detail">'=>'{block name="page-style-detail"}',
        // '<block name="page-content">'=>'{block name="page-content"}',
        '</block>'=>'{/block}',
        '</volist>'=>'{/volist}',
        '</empty>'=>'{/empty}'
        ];
          $content=$val;
        foreach ($list as $old => $new) {
              $content=str_replace($old,$new,$content);
          }
      echo('____________________________________________________________<br><br><br><br>');
      return $content;
    }



    //获取当前文件所在的绝对目录
    $dir =  dirname(__DIR__)."\application\console\\view";
    // p($dir);
    //扫描文件夹
    $file = scandir($dir);
    //显示

    $htmlarr=array(); // 把html文件加入到数组
    foreach ($file as $key => $name) {
        if(getExt2($name)=='.html'){
            array_push($htmlarr,$name);
        }
    }
    // p($htmlarr);
    // exit();
    $XiaoHeQQ496631085=0;
    foreach ($htmlarr as $key1 => $name) {
        // if($name!='Shopsz_page_set.html'){
        //     continue;
        // }


        $file_path = $dir.'/'.$name;
        p("打开".$file_path);
        $content=file_get_contents($file_path);
        
        $newcontent=tihuan1($content);
        

        echo "修改{$name}ing...<br>";
        $newcontent = re_tihuan($newcontent);
        
        $fugai=1;//覆盖源文件
        if($fugai){
            
            // p($newcontent);
            // exit();
            file_put_contents($file_path,$newcontent);
        }else{
            $dirz='newhtml';
            $dirc = iconv("UTF-8", "GBK", $dirz);
            if (!file_exists($dirc)){
                mkdir($dirc,0777,true);
                echo '创建文件夹'.$dirc.'成功';
            } 
            file_put_contents($dirz.'/'.$name,$newcontent);
            echo 'newhtml/'.$name.'</br>';
            // echo " <pre>";
            
                // echo "_______________________________________________________________________</br>";
            echo "<br>修改/newhtml/{$name}成功";
            // $newcontent = re_tihuan($newcontent);
        
        }
        $XiaoHeQQ496631085++;

    }
    echo "总共替换{$XiaoHeQQ496631085}个文件";

?>

上面注释里面 有相关的测试结果!

posted @ 2020-07-13 20:21  xiaohe520  阅读(937)  评论(0编辑  收藏  举报