1. First you need to open a file. Here's a PHP->fopen (); function
Definitions and usage
The fopen () function opens the file or URL.
If open fails, this function returns FALSE.
Function Prototypes:
fopen (Filename,mode,include_path,context)
Http://www.jb51.net/w3school/php/func_filesystem_fopen.asp.htm
Description
fopen () binds the name resource specified by filename to a stream. If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet registered the encapsulation protocol, PHP will issue a message to help check for potential problems in the script and continue with filename as a normal filename.
If PHP thinks that filename specifies a local file, it will attempt to open a stream on the file. The file must be accessible by PHP, so you need to confirm that the file access permission allows the access. If Safe mode is activated or open_basedir, further restrictions are applied.
If PHP believes that filename specifies a registered protocol and that the Protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is turned off, PHP will issue a warning and the fopen call fails.
The support for the context is added by PHP 5.0.0.
Tips and comments
Note: For portability considerations, it is strongly recommended that you always use the "B" flag when you open a file with fopen ().
2. After opening this file, read the file. PHP->fread () is used here; function
Definitions and usage
The Fread () function reads the file (which is safe for binary files).
Function Prototypes:
Fread (File,length)//remark: I didn't know. The file that this function obtains is the size of the file in bytes (byte) ....
Http://www.jb51.net/w3school/php/func_filesystem_fread.asp.htm
Description
Fread () reads up to length bytes from file pointer files. The function stops reading a file when it reads up to a maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or (after opening a user-space stream), reads 8,192 bytes, depending on which situation you first encounter.
Returns a string that is read, False if an error is returned.
Tips and comments
Tip: If you just want to read the contents of a file into a string, use file_get_contents (), which performs much better than Fread ().
Example 1
Reads 10 bytes from a file:
Copy Code code as follows:
<?php
$file = fopen ("Test.txt", "R");
Fread ($file, "10");
Fclose ($file);
?>
<?php $file = fopen ("Test.txt", "R"); Fread ($file, "10"); Fclose ($file);?>
Example 2
Read the entire file:
Copy Code code as follows:
<?php
$file = fopen ("Test.txt", "R");
Fread ($file, FileSize ("test.txt"));
Fclose ($file);
?>
<?php $file = fopen ("Test.txt", "R"); Fread ($file, FileSize ("test.txt")); Fclose ($file);?>
3. Start replacing template variables. Here's a php->str_replace (); function
Definitions and usage
The Str_replace () function replaces some other characters in a string with a string.
Function Prototypes:
Str_replace (Find,replace,string,count)
Http://www.jb51.net/w3school/php/func_string_str_replace.asp.htm
Tips and comments
Note: This function is sensitive to case sensitivity. Use Str_ireplace () to perform a search that is not case sensitive.
Note: This function is binary safe.
3. Replace the template variable. Use Php->echo (); function for output
Coding section:
Copy Code code as follows:
$title = "Test title";
$file = "Test Content";
Open this template
$tempdata =fopen ("test.html", "R");
Reading content in a template
$str =fread ($tempdata, FileSize ("test.html"));
Replacing content in a template
$str =str_replace (' {$title} ', $title, $STR);
$str =str_replace (' {$center} ', $file, $STR);
Output
Echo $str;