I’m trying to built a fully dynamic website. My client made a lot of websites with the same layout but with different content, for me the job to make it easy update-able. This means I have to adjust a part of my .htaccess file by editing it with a php script.
The problem is that I cannot find a solution for the following problem:
.htaccess has the following lines:
#O UD
Bladiebla usercontent
Bladiebla
Etc..
#C UD
The lines between the #O UD and #C UD lines should be updated. I thought I’d use the file_get_contents function, search and replace the lines with preg_replace and then save the file.
I’ve searched a lot but cannot find a working answer. What arguments do I use in the preg_replace function?
Look at this (it presumes you only have once occurrence of #O UD and #C UD).
<?PHP
/* the name of your file */
$file_name = "whatever.txt";
/* read the file contents into a string */
$contents = file_get_contents($file_name);
$beg_of = "#O UD";
$end_of = "#C UD";
/* remove all occurences of #O UD */
$contents = str_replace($beg_of, "", $contents);
/* remove all occurences of #O UD */
$contents = str_replace($end_of, "", $contents);
/* convert content to an array */
$lines = explode("\
", $contents);
/* remove first element as it is empty */
array_shift($lines);
/* remove last element as it is empty */
array_pop($lines);
/* create new content from the array */
$new_content = implode("\
", $lines);
/* do whatever you want with the results */
echo nl2br($new_content);
?>
Interesting approach! And it will work. Only, I really would like to know how to do this with preg_replace, haven’t found a solution yet. I’ll post it if I have one. If anyone has a new idea, feel free to post it.