php - 我需要在目录中的所有文件中自动替换我的代码的某些部分
问题描述
我正在尝试将大型网站转换为 PHP。所有文件都有几乎相似的头/页眉部分和页脚部分。
我试图通过将所有这些页眉部分和页脚部分放入一个单独的文件中来统一它们。
所以我需要更换我所有的
- 标题部分(跨越 1045 到 1535 行)
<?php include_once "include/header.php" ?>
和
- 页脚部分(跨越 80 到 140 行)
<?php include_once "include/footer.php" ?>
从目录中的所有文件..
这是我需要做的一个粗略的数字..请看下面的代码..
<!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>
在上面给出的代码中,我需要将所有内容从顶部替换为
<div class="menubar2">
(直到它关闭标签)
<?php include_once "include/header.php" ?>
另外,我需要替换页脚下方的所有内容
和
<?php include_once "include/footer.php" ?>
请帮我用我的字符串替换文件的那部分..
提前致谢..
解决方案
这是一个快速的粗略修复......没有测试它,但希望它有效!
快速抬头!如果您打算在<div>
内部<div class="menubar2"></div>
添加,请<div class="match-hidden"></div>
在.
首先改变</div>
class .menubar2
preg_match
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);
}
}
推荐阅读
- c# - 如何将代码隐藏 aspx.cs 数据转换为 javascript?
- java - 使用 JOIN-Table 的休眠多对多关联
- angular - Angular 5 & Jasmine - 如何模拟 activeRoute.firstChild 以在 ngOnDestroy 上测试退订
- android - Android chrome-cast 无法得到回调,当 android 设备在同一个 wifi 网络中并且在/不在 chrome-cast 范围内时
- ios - UIimage 大小与真实图像分辨率不同 (UIImageWriteToSavedPhotosAlbum)
- android-studio - 属性不显示
- javascript - jQuery在div onClick中加载内容
- android - 在特定时间调用 AlarmManager
- prolog - 非确定性谓词的 Prolog 单元测试
- html - 向 React-Native-Web 添加自定义字体