Home>Article>Backend Development> How to replace variables in php
php method to replace variables: first use "PHP ->fopen();" to open a file; then use "PHP ->fread();" to read the file; finally use "PHP-> ;str_replace();" function can replace template variables.
Recommended: "PHP Video Tutorial"
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, Dell G3 computer.
PHP steps to replace template variables
1. First you need to open a file. The PHP ->fopen(); function is used here
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)
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 that filename specifies a local file, it will try to open a stream on the 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....
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.
Return the read string, return 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:
The code is as follows:
Example 2
Read the entire file:
The code is as follows:
3. Start replacing template variables. The PHP->str_replace(); function is used here
Definition and usage
str_replace() The function replaces a string with other characters in a string.
Function prototype:
str_replace(find,replace,string,count)
Tips and notes
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:
The code is as follows:
$title="测试标题"; $file="测试内容"; //打开这个模板 $tempdata=fopen("test.html","r"); //读取模板中的内容 $str=fread($tempdata,filesize("test.html")); //替换模板中的内容 $str=str_replace('{$title}',$title,$str); $str=str_replace('{$center}',$file,$str); //输出 echo $str;
The above is the detailed content of How to replace variables in php. For more information, please follow other related articles on the PHP Chinese website!