dongqiangou5724 2019-05-12 07:20
浏览 50
已采纳

我需要在目录中的所有文件中自动替换部分代码

I'm trying to convert a large website into PHP. All the files have almost similar head/header portion and footer section.

I'm trying to unify all these header section, and footer section by putting it into a seperate file.

So I need to replace all of my

  1. header section (which spans anywhere from 1045 to 1535 lines) with
<?php include_once "include/header.php" ?>

And

  1. footer section (which spans anywhere from 80 to 140 lines) with
<?php include_once "include/footer.php" ?>

from all the files in a directory..

This is a rough figure of what I need to do.. Please look the code below..

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="">
        <meta name="description" content="">
        <meta name="author" content="">

        <title></title>

        <link rel="apple-touch-icon" href="">
        <link rel="shortcut icon" href="">

        <!-- Stylesheets -->
        <link rel="stylesheet" href=""> <!-- Some Stylesheets -->
        <link rel="stylesheet" href=""> <!-- Some Stylesheets -->
        <link rel="stylesheet" href=""> <!-- Some Stylesheets -->

        <!-- Scripts -->
        <script src=""></script>
    </head>
    <body class="">
        <nav class="nav1" role="navigation">
            <!-- Some HTML -->
        </nav>
        <div class="menubar1">
            <!-- Some HTML -->
        </div>
        <div class="menubar2">
            <!-- Some HTML -->
        </div>

        <!-- ========================================================= -->
        <!-- ========================================================= -->

        <!-- PAGE CONTENTS -->

        <!-- ========================================================= -->
        <!-- ========================================================= -->

        <!-- Footer -->
        <footer class="site-footer">
            <!-- Some HTML -->
        </footer>

        <script src=""></script> <!-- Some Script Tags -->
        <script src=""></script> <!-- Some Script Tags -->
        <script src=""></script> <!-- Some Script Tags -->
        <script src=""></script> <!-- Some Script Tags -->

        <!-- Page -->

        <!-- Google Analytics -->
        <script>
            /* Some Scripts */
        </script>
    </body>
</html>

In the above given code, I need to replace everything from top to

<div class="menubar2">

(Until it's closing Tag) with

<?php include_once "include/header.php" ?>

Also, I need to replace everything below footer

with

<?php include_once "include/footer.php" ?>

Please help me replace that portion of the file with my string..

Thanks in advance

  • 写回答

2条回答 默认 最新

  • duankuan5319 2019-05-12 07:57
    关注

    This is a quick rough fix... didn't test it but hope it works!
    Quick heads up! If you are planning on adding <div> inside <div class="menubar2"></div> then add this <div class="match-hidden"></div> after </div> closing tag of class .menubar2.
    Change first preg_match with
    preg_match('/<!DOCTYPE html>(.*?)<div class="match-hidden">(.*?)<\/div>/s',$file,$newfile);

    $file = file_get_contents('replace_head.html');
    preg_match('/<!DOCTYPE html>(.*?)<div class="menubar2">(.*?)<\/div>/s',$file,$newfile);
    preg_match('/<footer class="site-footer">(.*?)<\/html>(.*?)/s',$file,$newfile2);
    if($newfile == TRUE){
    $file1 = str_replace([$newfile[0]],['<?php include_once "include/header.php" ?>'],$file);
    if($newfile2 == TRUE){
    $file2 = str_replace([$newfile2[0]],['<?php include_once "include/footer.php" ?>'],$file1);
    //header('content-type:plain/text'); 
    //Un-comment this if you want to download current file
    header('content-type:application/json');
    //Comment this if you want to see it as html
    echo($file2);
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错