Jump to: navigation, search

PHP File Template

From w3cyberlearnings

Contents

PHP File Template

A text file can be used as a template.

File Content (book.txt)

This is a template text file that we are going to use with our template engine.

The book Infor: 
Author: {:author} 
Price:  {:price}
Publisher: {:publisher}
Title:		{:title}
Info:	{:body}	

Example 1

Use str_replace() function to replace string within the template with the user define string.

<?php

$file = 'book.txt';

$template_aa = array(
	 '{:author}',
	 '{:price}',
	 '{:publisher}',
	 '{:title}',
	 '{:body}');
$array_aa = array(
	 'Jannifer Bill',
	 30,
	 'Orailly',
	 'PHP/MySQL',
	 'It\' a book for Web Developer and MySQL');

if (file_exists($file)) {
	$file_content = file_get_contents($file);
	$fin_content = str_replace($template_aa, $array_aa, $file_content);
	print nl2br($fin_content);
}
?>

Output

The book Infor: 
Author: Jannifer Bill 
Price: 30
Publisher: Orailly
Title:	 PHP/MySQL
Info:	It' a book for Web Developer and MySQL

Related Links


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References