PHP Replacement template Variable implementation Step _php tutorial

Source: Internet
Author: User
1. First, you need to open a file. PHP->fopen () is used here; function
Definition and usage
The fopen () function opens the file or URL.
If open fails, this function returns FALSE.
Function Prototypes:
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 "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet been registered for encapsulation, PHP will issue a message to help examine potential problems in the script and continue with filename as a normal file name.
If PHP thinks that filename specifies a local file, it will attempt to open a stream on that file. The file must be accessible by PHP, so you need to confirm that the file access permission allows that access. If Safe mode is activated or open_basedir is applied, further restrictions apply.
If PHP believes that filename specifies a signed protocol, and that the protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is off, PHP will issue a warning, and fopen's call fails.
Support for the context is added by PHP 5.0.0.
Hints and Notes
Note: For portability, it is strongly recommended that you always use the "B" flag when opening a file with fopen ().
2. After opening this file, read the file. PHP->fread () is used here; function
Definition and usage
The Fread () function reads the file (which can be used safely in binary files).
Function Prototypes:
Fread (file,length)//Note: I didn't know. The file that this function obtains is the file size by byte (byte) ....
Http://www.jb51.net/w3school/php/func_filesystem_fread.asp.htm
Description
Fread () reads up to length bytes from file pointer files. The function stops reading the file when it has read up to the maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or when 8,192 bytes have been read (after opening a stream of user space), depending on which situation is encountered first.
Returns the read string if an error returns false.
Hints and Notes
Tip: If you just want to read the contents of a file into a string, use file_get_contents (), which has a much better performance than fread ().
Example 1
Read 10 bytes from a file:
Copy CodeThe code is as follows:
$file = fopen ("Test.txt", "R");
Fread ($file, "10");
Fclose ($file);
?>


Example 2
To read the entire file:
Copy CodeThe code is as follows:
$file = fopen ("Test.txt", "R");
Fread ($file, FileSize ("test.txt"));
Fclose ($file);
?>


3. Start replacing the template variable. Php->str_replace () is used here; function
Definition and usage
The Str_replace () function replaces some other characters in a string with a string.
Function Prototypes:
Str_replace (Find,replace,string,count)
Http://www.jb51.net/w3school/php/func_string_str_replace.asp.htm
Hints and Notes
Note: The function is case-sensitive. Use Str_ireplace () to perform a search that is not case sensitive.
Note: This function is binary safe.
3. After replacing the template variable. Use Php->echo (); function to output
Coding section:
Copy CodeThe code is as follows:
$title = "Test title";
$file = "Test Content";
Open this template
$tempdata =fopen ("test.html", "R");
Reading content from a template
$str =fread ($tempdata, FileSize ("test.html"));
Replace content in a template
$str =str_replace (' {$title} ', $title, $STR);
$str =str_replace (' {$center} ', $file, $STR);
Output
Echo $str;

http://www.bkjia.com/PHPjc/320560.html www.bkjia.com true http://www.bkjia.com/PHPjc/320560.html techarticle 1. First, you need to open a file. Php-fopen () is used here; function definition and usage the fopen () function opens the file or URL. If open fails, this function returns FALSE. Function Prototypes ...

  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.