Template_Read() - Parse the body from a webpage.

PHP Code
function Template_Read($FileSpec, $IsBase = null) {
	/*
	Purpose
		Used to read and parse a template file.
	Parameters
		FileSpec (string) = The filespec of the template file we want to read.
		IsBase (optional boolean) = Used to determine if this is a base template.
			If true, this is a base template.
			If false, this is not a base template.
			Default: false
	Return (string)
		If IsBase is true, this is a base template. Therefore, the body is not stripped out.
			Instead, the entire file is returned as is.
		If IsBase is false, this is not a base template. Therefore, the body is stripped out.
			Only the body of the file is returned.
	Notes
		If the opening <body> tag contains attributes, this function still parses properly.
	Version History
		1.00 2009.6.16 by Russ Tanner
	*/

	// Config
	$DELIMITER_BEGIN = '/<body.*>/i';					   // This is a regexp.
	$DELIMITER_END = '</body>';							 // This is just a string.

	// Default parameters
	if (is_null($IsBase)) $IsBase = false;				  // IsBase defaults to false

	// Read the template file
	$File = file_get_contents($FileSpec);

	// This is a base template. No need to parse the body out. Just return the entire file as is.
	if ($IsBase) return $File;

	// This is not a base template. Parse out the template portion of the file (the body).
	$Matches = '';										  // This will be loaded with an array by preg_match();
	$e = preg_match($DELIMITER_BEGIN, $File, $Matches);	 // We use a regexp in case <body> has attributes ("onload=")
	// Error Trap: An error occured in preg_match(). Display a bug trap.
	if ($e === false) {
		SayPage__BugTrap('In Template_Read(), preg_match() returned false');
		exit(1);
	}
	// Error Trap: No <body> tag was located in the template. Display a bug trap.
	if ($e == 0) {
		SayPage__BugTrap('In Template_Read(), no BODY tag was located within a template.<br />' . htmlspecialchars($FileSpec));
		exit(1);
	}

	$x = strpos($File, $Matches[0]) + strlen($Matches[0]);			  // Get a pointer to the end of the <body> tag, plus 1.
	$File = substr($File, $x);										  // Cut the start delimiter off
	$x = strpos($File, $DELIMITER_END) - 1;							 // Get the end-cut position
	$File = substr($File, 0, $x);									   // Cut the end delimiter off

	return $File;

}
 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)