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 : 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 : 3858
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 
 

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;
?>



YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
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
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
FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, 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
Simple Template Class/Example
Categories : PHP, Templates, PHP Classes
Very minimal templating engine
Categories : PHP, PHP Classes, Templates
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed
Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS)
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables