Home > Backend Development > PHP Tutorial > PHP steps to replace template variables_PHP tutorial

PHP steps to replace template variables_PHP tutorial

WBOY
Release: 2016-07-21 15:44:17
Original
1083 people have browsed it

1. First you need to open a file. PHP is used here ->fopen(); function
Definition and usage
fopen() function opens a file or URL.
If the 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 of the form "scheme://...", it is treated as a URL and PHP will search for a protocol handler (also called a wrapper protocol) to handle the scheme. If the wrapper protocol has not been registered for the protocol, PHP will emit a message to help check for potential problems in the script and continue execution of filename as if it were a normal filename.
If PHP thinks filename specifies a local file, it will try to open a stream on that file. The file must be accessible to PHP, so you need to confirm that the file access permissions allow this access. If safe mode or open_basedir is activated further restrictions apply.
If PHP believes that filename specifies a registered protocol, and the protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If it is closed, PHP will issue a warning and the call to fopen will fail.
Support for context was added in PHP 5.0.0.
Tips and Notes
Note: For portability reasons, it is strongly recommended to always use the "b" flag when opening files with fopen().
2. After opening this file, read the file. PHP ->fread(); function is used here
Definition and usage
fread() function reading file (safe for use with binary files).
Function prototype:
fread(file,length) //Note: I just know. The file obtained by this function calculates the file size in bytes....
http: //www.jb51.net/w3school/php/func_filesystem_fread.asp.htm
Description
fread() reads up to length bytes from the file pointer file. This function is called after reading up to length bytes, or when EOF is reached, or (for network streams) when a packet is available, or (after opening a user space stream) 8192 bytes have been read. Will stop reading the file, depending on which condition is encountered first.
Returns the string read, or returns false if an error occurs.
Tips and Notes
Tip: If you just want to read the contents of a file into a string, please use file_get_contents(), which has much better performance than fread().
Example 1
Read 10 bytes from the file:

Copy the code The code is as follows:

$file = fopen("test.txt","r");
fread($file,"10");
fclose($file);
?> ;


Example 2
Read the entire file:
Copy the code The code is as follows:

$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>


3. Start replacing template variables. PHP->str_replace() is used here; function
Definition and usage
str_replace() function uses a string to replace characters 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. Please use str_ireplace() to perform a case-insensitive search.
Note: This function is binary safe.
3. After replacing the template variables, use the PHP->echo(); function to output
Encoding part:
Copy code The code is as follows :

$title="Test title";
$file="Test content";
//Open this template
$tempdata=fopen("test.html" ,"r");
//Read the content in the template
$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;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320560.htmlTechArticle1. First you need to open a file. PHP -fopen() is used here; function definition and usage fopen() Function opens a file or URL. If the opening fails, this function returns FALSE. Function prototype...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template