Here's a better way. This solves the problem of not knowing what the actual content that is going to change looks like when using a regex.
- Create a file that looks like this:
main.php
// Static content 1 here
<!-- start -->
// Dynamic content 1 here
<!-- end -->
// Static content 2 here
- Create a file with new content:
new.txt
<!-- start -->
// Dynamic content 2 here
<!-- end -->
- Create a temporary file:
temp.txt
- Create the PHP script:
replace.php
<?php
// Open all three files
$fp = fopen("/home/main.php", "r");
$fp2 = fopen("/home/new.txt", "r+");
$fp3 = fopen("/home/temp.txt", "w+");
// Read the two files into a string
$m = fread($fp, filesize("/home/main.php"));
$n = fread($fp2, filesize("/home/new.txt"));
// Split the contents of main.html
// $array[0] contains Static content 1
// $array[1] contains Static content 2
$array = split("<!-- start -->(.*)<!-- end -->", $m);
// Now join the three separate strings
$new_string = $array[0].$n.$array[1];
// Write the new string to temp.txt
fwrite($fp3, $new_string);
/
temp.txt should look like this now
// Static content 1 here
<!-- start -->
// Dynamic content 2 here
<!-- end -->
// Static content 2 here
/
// Close the files
fclose($fp1);
fclose($fp2);
fclose($fp3);
?>
- You can than write a forced cp call or something to copy temp.txt to main.html.