Hello,
I have a php script that generates a series of html files using an html template file and a text file of key phrases. The html contains several places where I need to insert the keyword that is read in from the text file in my php code. So the template html would look something like this
<h2>%term% at YourSite.com</h2>
<p>If you are are looking for %term% you have come to the right place. YourSite.com is the #1 place for %term%.</p>
where %term% marks the spot I need my variable inserted. So I created the following php script to make this work, but it's very much a brute force technique that I would like to improve on.
$keywords = "keywords.txt";
$template = "template.txt";
$file = fopen ("template.txt", "r") or die("Can't open template file");
$tmp = fread ($file,1000000);
fclose ($file) or die("Can't close template file");
$content = file($keywords); //Read in the keywords from the text file
$template_array = explode("%term%",$tmp) or die("Can't open template file");
$counter = count($template_array);
while (list(, $kw_value) = each ($content)) {
$i = 0;
while (list(, $value) = each ($template_array)) {
$i++;
print $value;
if ($i < $counter) {
print $kw_value;
}
}
exit();
}
}
This script reads the key words into an array and generates a page for each of them using the html template. It "works" but I don't like the way I had to use the explode function to read the template into an array delimted by %term%. I would prefer to read in the template file and parse it on the fly, replacing the %term% variable with $kw_value whenever it occurs.
Can anyone help me out with a method of doing this?
-Chris