Mind that this is a solution which works for me. It takes a html-file, inserts a couple of lines of code after the body tag and saves it as the same file but with the .php extension. It might be usefull for you, it might be useless. Let me know if you have any remarks...
I didn't mind about making it really re-useable. If anybody knows how it would be possible to ameliorate the code, feel free to let me know to. I'm always eager to learn!
FUNCTION insert_code($file) {
// inserts the code
// open the file
$filechunks = explode(".", $file);
$t=0;
while ($t < count($filechunks)-1) {
$fileprefix .= $filechunks[$t] . ".";
$t++;
}
//echo "count:" . count($filechunks) . "<br>file:$file<br>fileprefix:$fileprefix";
//exit;
$filesuffix = $filechunks[count($filechunks)-1];
if ($filesuffix <> "html") {
return false;
} // do not proces this file
$fp = fopen($file, "r");
$filecontent = fread($fp, filesize($file));
fclose($fp);
$searchstring = "<body";
$code = "YOUR CODE HERE";
// find first occurence
$position = strpos($filecontent, $searchstring);
if ($pos === false) { // note: three equal signs
// not found...
echo "Not found";
}
$endstringposition = strpos($filecontent, ">", $position) + 1;
$first_part = substr($filecontent, 0, $endstringposition);
$last_part = substr($filecontent, $endstringposition);
$new_content = $first_part . $code . $last_part;
$new_fp = fopen($fileprefix . "php", "w");
fwrite($new_fp, $new_content);
fclose($new_fp);
}