WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 3 of 5 (graded 3 times)
Viewed : 11816
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  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
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
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem
A simple configuration file editor to ease you life in setting up php applications. Reads variables from a given file automatically and displays current value. New value will be written to file after submit.
Categories : PHP, Filesystem, Regexps, Java Script
Easy upload class
Categories : PHP Classes, Filesystem, HTTP, PHP
How to create an empty file? (touch)
Categories : Filesystem, PHP
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
Read DPI value from image with PHP
Categories : PHP, Graphics, Filesystem
Fetching product details from the commission junction website using php
Categories : PHP, FTP, Filesystem, Compression
Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
Execute a command to a string
Categories : PHP, Program Execution, Filesystem