1. Open a file first. Here PHP-> fopen (); function is used.
Definition and usage
The fopen () function opens a file or URL.
If opening fails, this function returns FALSE.
Function prototype:
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 in the format of "scheme: //...", it is treated as a URL. PHP will use the search protocol processor (also known as the Encapsulation Protocol) to process this mode. If the Protocol has not yet registered the encapsulation protocol, PHP will send a message to help check the potential problems in the script and continue executing the filename as a normal file name.
If PHP considers filename to be a local file, it will try to open a stream on the file. This file must be accessible by PHP. Therefore, you need to confirm that the file access permission permits this access. If the security mode or open_basedir is activated, further restrictions are applied.
If PHP considers filename to be a registered protocol registered as a web URL, PHP checks and confirms that allow_url_fopen is activated. If it is disabled, PHP will issue a warning, while the fopen call will fail.
The support for context is added in PHP 5.0.0.
Tips and comments
Note: For portability considerations, we strongly recommend that you always use the "B" flag when opening a file with fopen.
2. open the file and read the file. Here PHP-> fread (); function is used.
Definition and usage
Fread () function reads files (which can be safely used for binary files ).
Function prototype:
Fread (file, length) // Note: I only know that the file obtained by this function is calculated by Byte ....
Http://www.jb51.net/w3school/php/func_filesystem_fread.asp.htm
Description
Fread () reads a maximum of length bytes from the file pointer. This function can be read by a maximum of length bytes, or when it reaches the EOF, or (for network streams) when a package is available, or (after opening the user space Stream) when 8192 bytes have been read, the system stops reading files.
Returns the read string. If an error occurs, false is returned.
Tips and comments
Tip: to read the content of a file into a string, use file_get_contents (). Its performance is much better than that of fread.
Example 1
Read 10 bytes from the file:
Copy codeThe Code is 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 codeThe Code is 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 to replace the template variable. Here PHP-> str_replace (); function is used.
Definition and usage
The str_replace () function uses one string to replace other characters in the string.
Function prototype:
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 case sensitive. Use str_ireplace () to perform a case-insensitive search.
Note: This function is binary secure.
3. Replace the template variables. Use PHP-> echo (); function to output
Encoding:Copy codeThe Code is as follows: $ title = "test title ";
$ File = "Test content ";
// Open this template
$ Tempdata = fopen ("test.html", "r ");
// Read the template content
$ Str = fread ($ tempdata, filesize ("test.html "));
// Replace the content in the template
$ Str = str_replace ('{$ title}', $ title, $ str );
$ Str = str_replace ('{$ center}', $ file, $ str );
// Output
Echo $ str;