Home  >  Article  >  Backend Development  >  How to replace template variables in php

How to replace template variables in php

藏色散人
藏色散人Original
2021-03-08 09:26:501977browse

How to replace template variables in php: 1. Use the fopen() function to open the file; 2. Read the file through the fread function; 3. Use the str_replace function to replace the template variables, the syntax "str_replace(find, replace, string, count)".

How to replace template variables in php

#The operating environment of this article: Windows 7 system, PHP8, 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 One stream up. 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

Definition and usage

is used here

fread() function reads files (safe for 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

Tips: If you just want to read the contents of a file into a string, please use file_get_contents(), its performance is much better than fread().

Example 1: Read 10 bytes from the file:

<?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:

<?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 .The PHP->str_replace(); function is used here

Definition and usage

str_replace() function uses a string to replace other characters in the string.

Function prototype:

str_replace(find,replace,string,count)

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:

$title="测试标题"; 
$file="测试内容"; 
//打开这个模板 
$tempdata=fopen("test.html","r"); 
//读取模板中的内容 
$str=fread($tempdata,filesize("test.html")); 
//替换模板中的内容 
$str=str_replace(&#39;{$title}&#39;,$title,$str); 
$str=str_replace(&#39;{$center}&#39;,$file,$str); 
//输出 
echo $str;

[Recommended: "PHP Video Tutorial》】

The above is the detailed content of How to replace template variables in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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