Okay, so the concept behind directly editing an HTML file is you would have a placeholder in the HTML file you want to replace.
Example:
HTML Code:
<html>
...
<h1 class="title">{title}</h1>
<p class="description">{description}</p>
...
</html>
Then in PHP, you would read the HTML file, replace the placeholder with the user entered content, and save it back into the file.
PHP Code:
<?php
// checks to see if the user submitted the form
if (isset($_POST['generate']))
{
$title = '';
$description = '';
// looks for the title input field
if (isset($_POST['title']))
{
// escapes any quotes so they are stored properly within the configuration file
$title = addslashes($_POST['title']);
}
// looks for the description field
if (isset($_POST['description']))
{
// escapes any quotes so they are stored properly within the configuration file
$description = addslashes($_POST['description']);
}
$htmlFileContents = file_get_contents('myhtmlfile.html');
$htmlFileContents = str_replace('{title}', $title, $htmlFileContents);
$htmlFileContents = str_replace('{description}', $description, $htmlFileContents);
$result = file_put_contents('myhtmlfile.html', $htmlFileContents);
if ($result === false)
{
die('Writing of the file failed.');
}
}
?>
Now there are more sophisticated ways of allowing to alter the existing HTML file, you can use a regular expression to find the <h1 class="title">...</h1> tag and replace it with a new one, same for the paragraph tag. My regular expression writing isn't the best, so if you'd want to go that route so you could use this process instead of the variable process, you may want to branch out to someone who has better regular expression skills than I.
Bookmarks