Having done more than a fair share of website design in PHP one of the issues that I run across occasionally is that some of the folks I design for like to be able to edit there own webpages using various editors like Frontpage, Dreamweaver and so on.
But while they enjoy editing and updating there sites using various wysiwyg editors, they don't have the knowledge of PHP to be able to effectively edit it if it's part of the HTML file.
I do a great deal of my own webdesign using Frontpage, and flipping back and forth from the design view to the code view can be a pain for me as well. It gets a bit cumbersome when your redesigning a page to have to cut and paste various portions of PHP from one section to another so that whatever dynamic content I'm working with gets posted to the page properly. It is also a hassle when I rename any of the files with the extension .PHP because Frontpage doesn't acknowledge this as an extension.
I came up with this as a solution, a PHP script that allows you to read in an HTML template and make substitutions on the fly. That way you can keep your HTML in a nice, easy to edit template that can be easily edited by any of the wysiwyg editors on the market, and the PHP coding portions are nice, seperate entities. I've found this also helps save a lot of time when I'm updating the PHP entry's, as I'm not searching through tons of HTML trying to find the php code I'm looking for.
Here is the main workhorse of the script, an include file I named hparse.inc.php:
<?php
//HTML Parser for PHP
//Reads in HTML File and outputs it with info from PHP
$html_var=array();
$php_var=array();
$find_array=array();
$replace_array=array();
function read_html($filename)
{
$fh = fopen($filename, 'r');
$html_in = fread($fh, filesize($filename));
fclose($fh);
return ($html_in);
}
function replace_var($var_name, $var)
{
global $html_var, $php_var;
$html_var[]=$var_name;
$php_var[]=$var;
}
function replace_html($find, $replace)
{
global $find_array, $replace_array;
$find_array[]=$find;
$replace_array[]=$replace;
}
function parse_html($html)
{
global $find_array, $replace_array, $html_var, $php_var;
$out=read_html($html);
$out=str_replace($html_var, $php_var, $out);
$out=str_replace($find_array, $replace_array, $out);
return($out);
}
?>
So how is it used? Lets say we have a very simple HTML file were using named test.html, and it looks something like this:
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<p>!Variable</p>
</body>
</html>
We've used our wyswig editor to create this template file, and we've specified a variable name we want to replace with content from PHP, in this case we've used the designator !Variable to be our variable in html. The script is dynamic enough You can use almost anything for this variable, but I recommend that you stick with a ! followed the name of the variable you wish to replace in the HTML file to avoid accidental replacements. Technically speaking you can designate this variable anyway you wish, even $Variable in the HTML could work, but passing a $ character in PHP can be a pain so I generally just stick with !.
Now that we have our HTML template, were going to use PHP to read it in and replace the !Variable in the HTML with some dynamic content. In this example we'll keep it simple and just use the string "It Works!" to replace !Variable in the HTML.
So, we create a php file that will read in our html template and replace all of the occurances of !Variable with a string "It Works!" and then outputs the formated html with the variable replaced:
<?php
include "hparse.inc.php";
replace_var("!Variable","It Works!");
//we give the replace var two parameters, the first is the variable we wish to
//replace in the html file, the second is the string or value we want to replace it
//with
$test=parse_html("test.html");
//the parse_html command reads in the html file, makes any substitutions we
//designated earlier using either replace_var or replace_html and returns the
//entire html template to a string, that we can then echo out
echo $test;
?>
So whats the replace html function do? rather than defining a variable to replace, it allows you to replace any portion of the html in your template with something else. For example, lets say in this case we wanted to use this exact same template for 3 or 4 different pages, but we wanted to change the title in the html.
<?php
include "hparse.inc.php";
replace_var("!Variable","It Works!");
replace_html("<title>New Page 1</title>", "<title>It Works</title>", );
$test=parse_html("blog.html");
echo $test;
?>
If you wish you can specify mulitiple variables or multiple html segments using replace_var or replace_html prior to calling the parse_html command, making these functions very flexible and rather easy to use.
I've found this particular function to be extremely valuable, hopefully you will as well. Comments, questions and suggestions are always welcome, but please, no throwing of rotten vegtables, I hate that