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 : Parsing Simple Template Files and Data
Categories : PHP, PHP Classes, Templates, Regexps Click here to Update Your Picture
MA Razzaque Rupom
Date : Jul 16th 2006
Grade : 3 of 5 (graded 2 times)
Viewed : 10590
File : 4469.zip
Images : No Images for this code example.
Search : More code by MA Razzaque Rupom
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This class implements a simple template processor engine based on regular expression matching.

It can process template strings or files.

It uses regular expressions to find place holder marks and a callback function to replace the marks by the respective template parameter values.


TemplateParser.class.php
<?php
/**
* Simple Template Parser Class
*
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource Group(http://groups.yahoo.com/group/phpresource/)
*             URL: http://www.rupom.info
* @date    : 07/17/2006 
* @version :  1.0
* Purpose  :  Parsing Simple Template File and Data that Contains Macros
*/

class TemplateParser
{
     var
$data;
     
     
/**
     * Initializes "macro=>value" array
     * @param Array "macro=>value" array
     * @return none
     */
   
function initData($data)
   {
     
$this->data = array();       
     
$this->data = $data;       
   }
   
   
/**
     * Parses template file
     * @param template filename
     * @return parsed template
     */
   
function parseTemplateFile($templateFile)
   {
     
$searchPattern          = "/\{([a-zA-Z0-9_]+)\}/i"; // macro delimiter "{" and "}"
     
$replacementFunction    = array(&$this, 'parseMatchedText');  //Method callbacks are performed this way
     
$fileData               = file_get_contents($templateFile);                       
     
$parsedTemplate         = preg_replace_callback($searchPattern, $replacementFunction, $fileData);
     
      return
$parsedTemplate;
   }

   
/**
     * Parses template data
     * @param template data
     * @return parsed data
     */
   
function parseTemplateData($templateData)
   {
     
$searchPattern          = "/\{([a-zA-Z0-9_]+)\}/i"; //macro delimiter "{" and "}"
     
$replacementFunction    = array(&$this, 'parseMatchedText');  //Method callbacks are performed this way       
     
$parsedTemplate         = preg_replace_callback($searchPattern, $replacementFunction, $templateData);
     
      return
$parsedTemplate;
   }
   
   
/**
   * Callback function that returns value of a matching macro
   * @param Array $matches
   * @return String value of matching macro
   */
   
function parseMatchedText($matches)
   {
      if(isset(
$this->data[$matches[1]]))
      {
         return
$this->data[$matches[1]];
      }
      return
'{'.$matches[1].'}';    //wont be parsed if no value is set for the macro
   
}

}
//End Of Class

?>



Usage Example
<?php
/**
* Usage of Simple Template Parser Class
*
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource Group(http://groups.yahoo.com/group/phpresource/)
*             URL: http://www.rupom.info 
* @date    : 07/17/2006 
* @version :  1.0
* Purpose  :  Parsing Simple Template File and Data that Contains Macros
*/

require_once "TemplateParser.class.php";
$data = array();
$data['first_name']  = "MA";
$data['last_name']   = "Razzaque";
$data['nick_name']   = "Rupom";
$data['password']    = "supersecret";
$data['sender_name'] = "Administrator";
$data['site_name']   = "Name_Of_Site";

$obj = new TemplateParser();
$obj->initData($data);

//Parse Template File
$parsedData = $obj->parseTemplateFile("template.html");
echo
$parsedData;

echo
"<br>-----------------------------------<br>";

//Parse Template Data
$parsedData = $obj->parseTemplateData(file_get_contents("template.html"));
echo
$parsedData;
?>



Very minimal templating engine
Categories : PHP, PHP Classes, Templates
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
EAvalidator - This class can be used to validate an e-mail address by checking its domain.
Categories : PHP, PHP Classes, Email, Regexps
The GTV.class allows you to extract a value between any HTML tag or between any TEXT on a web page.
Categories : PHP, PHP Classes, Regexps
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, PHP Classes, Regexps
Simple Template Class/Example
Categories : PHP, Templates, PHP Classes
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps
STR - a Perl-like string manipulator class - The str class provides 4 perl-like methods for manipulating strings and other scalar variables.
Categories : PHP, PHP Classes, Perl, Strings
SubmitForce URL power submitter (searchengine submission class)
Categories : PHP, Search Engines, URLs, PHP Classes
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
XPath for PHP without the DOM XML extension
Categories : DOM XML, XML, XSLT, PHP Classes, PHP
PHP Domain Availability Checker
Categories : PHP, Complete Programs, Regexps, HTTP, Sockets