PHP page static technology; study notes

Source: Internet
Author: User

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Demo</title></Head><Body>        <Pstyle= "color: #ff9000;">%demo%</P></Body></HTML>

This is the page that needs to be replaced

The static of the PHP page is very necessary, especially in the CMS system, some content once generated, basically will not change, then if the page is static with HTML, it will undoubtedly reduce the burden of Service parsing PHP page. The following is a book to learn about the static PHP technology, record for a rainy-night.

Whether using a framework or a simple script, the principle is basically the same: the use of PHP for file operations, replacing the dynamic elements in HTML templates, not only to improve the user's access speed can also make PHP and HTML separation, so that the code is more maintainable.

File operation function used:

fopen ()--------------------open a file function has two parameters: 1. The path and file name of the open file 2. The mode in which the file was opened (in string form) used here R and W R for Read only W for write only

Feof ()--------------------read the file until the end of the file this function returns True at the end of the file, so you can use while to know when to finish reading the file; The function has a parameter, 1. Open file pointer

Fwiter ()--------------------write the contents of the file, the function has three parameters: 1. File name to write 2. Content written 3. (optional) Maximum number of characters written

Fclose ()------------------------close the file; You must close the file after you have finished all the operations;

Note: You need to pay attention to the path of the file, and the permissions required to read the file;

PHP Code:

<?PHPHeader("Content-type:text/html,charset=utf-8");/******php static *******/    classtexthtml{ Public functionFwritehtml ($r){            /*PHP page Static technology * $r [array];             *route=> generate the file name and path *html=> the static resource that needs to be read *stsyem=> the label to replace *content=> label replacement content * */                        $cfill=fopen($r["HTML"], "R");//the Read file name            $cfillnew=fopen($r["Route"], "w");  while(!feof($cfill)){                $row=fgets($cfill);

$res=Str_replace($r["Stsyem"],$r["Content"],$row);//Replace content
          fwrite($cfillnew,$res);//Writing HTML files } /*Close File*/ fclose($cfill); fclose($cfillnew); } } $f=Newtexthtml; $fillname=Microtime().". html; $content= "This is a page static technology"; $r["route"] =$fillname; $r["html"] = "index.html"; $r["Stsyem"] = "%demo%"; $r["content"] =$content; $f->fwritehtml ($r);?>

The final results are as follows:

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>National City level Three linkage</title></Head><Body>        <Pstyle= "color: #ff9000;">This is a page static technology</P></Body></HTML>

The use of PHP static technology can better achieve the Web page function, in the creation of articles or a few changes in the content can be used;

PHP page static technology; study notes

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.