我需要在目录中的所有文件中自动替换部分代码
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
- header section (which spans anywhere from 1045 to 1535 lines) with
<?php include_once "include/header.php" ?>
And
- 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
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);
}
}
First, note that regular expressions should generally never be used with DOM. However, since we're talking mass replacing within files, I suppose this is acceptable in this case (you don't want to lose the formatting, and mass deleting nodes with DOM would be awkward as well).
Now, taking into account what you said in the other comment about the structure of your menubar2
div, this should do it.
Edit: now with dynamic number of divs under menubar2
:
// Compute the amount of divs within `menubar2` div
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($html);
$menubar2_inner_div_count = (new \DOMXPath($doc))
->query('//div[@class="menubar2"]//div')
->length;
// Header
$html = preg_replace(
'/^.*?<div class="menubar2">' . str_repeat('.*?<\/div>', $menubar2_inner_div_count + 1) . '/s',
'<!-- Header -->' . PHP_EOL . '<?php include_once "include/header.php" ?>',
$html
);
// Footer
$html = preg_replace(
'/<footer class="site-footer">.*?<\/html>/s',
'<?php include_once "include/footer.php" ?>',
$html
);
// (Optional) Remove now useless indentation (supposes your indent is 8 spaces,
// otherwise replace accordingly)
$html = preg_replace('/^ {8}/m', '', $html);
Demo: https://3v4l.org/G3HbK