WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Functions to read a template file and fill in PHP variables. It will also fill in array variables, displaying parts of the template multiple times.
Categories : PHP, Variables, Filesystem Update Picture
Auke Slooten
Date : Jan 17th 1999
Grade : 2 of 5 (graded 1 times)
Viewed : 7152
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Auke Slooten
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
// HTML Template v0.10
//
// Changes from HTML Templates v0.9
//
// AddSlashes replaced by ereg_replace("\"","\\\"...
// to prevent single quotes from getting escaped too (javascript errors)
//
// Added EvalTemplate which returns the filled in template, this way
// it's now possible to create any kind of template e.g. mail
//
// What is HTML Templates v0.10
//
// A function that fills in php variables in a template file to make
// the design of your website independent of the php code.. I hope.
// Call: "$template=EvalTemplate($templatefile, $vars);"
// WARNING: no warranty whatsoever, use at your own risk, there will be bugs
// and/or security risks in this code.
//
// $templatefile is a full filename of the html file to use as a template.
//
// $vars is a list of all the variables used in the templatefile. It is
// needed since all global variables need to be declared in the function.
// The format is "\$varname" [ ", \$varname" ]* e.g.: "\$var1, \$var2"
//
// The template file can contain any html-code. Variables can be included
// anywhere like this: '<body bgcolor="$bgcolor1">'
// If you want to display some html code multiple times for array variables
// use something like the following format:
// <select name="test"> {
// <option value="$test[]">$test[]</option>
// } </select>
//
// Mistyping this like {{ $test[] } will cause strange errors as php tries
// to interpret $test[]. It will complain that is needs a STRING or an INT or
// something as it wants an index key between [ and ] :)
//
// Security.
// I don't check for /dev/zero, or other large template files, if you can
// choose the template file, you obviously can already create malicious php
// files.
// The template files should be safe, although every line is incuded in an
// 'eval' statement. I replace every '"' with a '\"', so you shouldn't be
// able to 'escape' from the echo statement in a template file.
//
// Used variables.
// Since the function 'globals' any variables you defined in your template
// it is easy to get a variabele name clash. Here's the list of allt he
// variables I used in this function:
// $templatefile, $line, $vars: function declarations
// $dt_result
// $dt_middle
// $dt_explode
// $dt_multiple
// $dt_expl2
// $dt_uservar
// $dt_uservar_count
// $dt_i
// $dt_temp
// $dt_tail
// $dt_template
//
// copyright 1998 Auke van Slooten
// released under the GNU Genereal Public License
function EvalLine($line, $vars) {
eval( "global $vars;");
if (ereg( "[^{]{[^{}][^}]*}",$line, $dt_result)) { // found a multiple
$dt_middle=substr($dt_result[0],1,strlen($dt_result[0])-1);// cut the "[^{]" character
$dt_explode=explode($dt_middle, $line); // explode on the multiple part

$dt_evalhead=EvalLine($dt_explode[0], $vars); // no more multiples here
$dt_multiple=substr($dt_middle,1,strlen($dt_middle)-2); // cut "{" and "}"

$dt_expl2=explode( "[]",$dt_multiple);
$dt_uservar=strrchr($dt_expl2[0], "\$"); // get the first array variable name

eval( "\$dt_uservar_count=@count($dt_uservar);"); // count the length of the array
// @ suppresses warnings when the array is not defined

// this line replaces things like $var[] with $var[$dt_i].
$dt_multiple=ereg_replace( "\\\$([^\[]*)\[\]", "\$\\1[\$dt_i]",$dt_multiple);
for ($dt_i=0;$dt_i<$dt_uservar_count;$dt_i++) {
eval( "\$dt_evalmultiple.=\"$dt_multiple\";"); // let eval fill in the values
}
$dt_explode[0]= ""; // first part is done, remove it
$dt_temp=implode($dt_explode, $dt_middle); // and glue the parts together again
$dt_tail=substr($dt_temp, strlen($dt_middle), strlen($dt_temp)); // cut the first "middle" part of,
// its in dt_evalmultiple now

// could also go for EvalLine($dt_explode[1], $vars) but there might be more elements...
return $dt_evalhead.$dt_evalmultiple.EvalLine($dt_tail, $vars);
} else {
// no multiples, just replace double {{ with a single { and let eval fill in the values for variables
eval( "return \"".ereg_replace( "{{", "{",$line). "\";");
}
}

Function EvalTemplate($templatefile, $vars) {
$dt_template=ereg_replace( "\"", "\\\"",implode(file($templatefile), ""));
return EvalLine($dt_template, $vars);
}

Function DisplayTemplate($templatefile, $vars) {
echo EvalTemplate($templatefile, $vars);
}
?>



Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Working with files - return an array of files within a directory
Categories : PHP, Strings, Variables, Filesystem
Working with files - putting file contents to a string / var
Categories : PHP, Filesystem, Variables, Strings
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Introduction to Language Files
Categories : PHP, Filesystem, Beginner Guides
A flat file counter
Categories : PHP, Cookies, Filesystem, Beginner Guides
Show Source with Line Numbers
Categories : PHP, Regexps, Filesystem
The following snippet gives complete info about all submitted HTTP_POST_VARS and HTTP_GET_VARS
Categories : Variables, HTTP, PHP
clearing variables in php3
Categories : Variables, Arrays, PHP
How to display a PHP variable value from a selectbox without reloading the page by merging PHP and Javascript variables.
Categories : PHP, Java Script, Variables
Creating a Language File
Categories : PHP, Beginner Guides, Filesystem
Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
Categories : PHP, Directories, Variables, Arrays
translate.php - Assocciative array example, passing a reference to a function.
Categories : PHP, Arrays, Languages, Variables
getting the name of the current script and query string
Categories : PHP, Global Variables, Variables, URLs
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables