For more information about how to use PHP to replace Template variables, see.
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.bitsCN.com/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.bitsCN.com/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:
The code is as follows:
$ File = fopen ("test.txt", "r ");
Fread ($ file, "10 ");
Fclose ($ file );
?>
Example 2
Read the entire file:
The code is as follows:
$ 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.bitsCN.com/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:
The 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;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.