Home Backend Development PHP Tutorial Analysis of two methods of php static page generation

Analysis of two methods of php static page generation

Jul 25, 2016 am 08:59 AM

  1. // Method 1, generate a static page based on the template

  2. // The replaceTemplateString function is used to replace the specified string in the template
  3. function replaceTemplateString($templateString) {
  4. // Use Variables to replace
  5. $title = "Article title";
  6. $body = "Here is the body of the article";
  7. // Replace the string specified in the template
  8. $showString = str_replace ( "%title%", $title, $templateString );
  9. $showString = str_replace ( "%body%", $body, $showString );
  10. // Return the replacement result
  11. return $showString;
  12. }

  13. $template_file = " template.html";

  14. $new_file = "new.html";
  15. // Template file pointer
  16. $template_juBing = fopen ( $template_file, "r" );
  17. // File pointer to be generated
  18. $newFile_juBing = fopen ( $ new_file, "w" );

  19. // Method 1, get the overall template content string, replace it and assign it to the new file

  20. $templateString = fread ( $template_juBing, filesize ( $template_file ) ) ;
  21. $showString = replaceTemplateString ( $templateString ); // Replace the string in the template
  22. fwrite ( $newFile_juBing, $showString ); // Write the replaced content into the generated HTML file

  23. // Method 2, read each line of the template content string in a loop, replace it and add it to the new file in turn
  24. while ( ! feof ( $template_juBing ) ) { // The feof() function detects whether the end of the file has been reached. Returns TRUE if the file pointer reaches the end or an error occurs. Otherwise, return FALSE (including socket timeout and other situations).
  25. $templateString = fgets ( $template_juBing ); // fgets(file,length) reads a line from the file pointer and returns a string up to length - 1 bytes long, including newlines. If length is not specified, it defaults to 1K, or 1024 bytes.
  26. $showString = replaceTemplateString ( $templateString );
  27. fwrite ( $newFile_juBing, $showString ); // When writing content to the opened pointer file for the first time, the original content in the pointer file will be replaced. Before the file pointer is closed, If the fwrite function adds content, it will close the file pointer after the added content
  28. }
  29. */
  30. //
  31. fclose ( $newFile_juBing );
  32. fclose ( $template_juBing ); Relationship with static pages
  33. Usually, after adding a piece of information in the database, a static page of the information is generated, so it is best to add a field in the database table to store the path file name of the corresponding static page to facilitate future modifications. Delete < /p>
  34. Template replacement

  35. Generally speaking, if you need to modify the template of a static HTML page, the usual approach is to delete all the generated HTML pages and then recreate the new HTML page. (Or all re-generated)

  36. Dynamic operations on static pages

  37. Sometimes, some dynamic operations also need to be performed on the static HTML pages created. For example, the click-through rate of each news article in the news system is counted.
  38. You can use an image control with a width and height of 0 pixels to hide a php page to implement the page counter function, such as
  39. Static page of link directory

  40. Usually for systems that use static pages, static HTML files are often generated for the directory page of the link list for visitors to browse
  41. Note This is because every addition or deletion of database information will have an impact on the link list. Therefore, every time database information is added or deleted, the static page of the link directory needs to be updated.
  42. Paging design can be completed by creating multiple static pages with linked directories.
  43. */

  44. // Method 2, generated based on the buffer

  45. ob_start (); // When the buffer is activated and there is ob_end_clean(), all non-file output is printed The header information will not be printed to the page, but will be saved in the internal buffer. If there is no ob_end_clean(), the information is both stored in the internal buffer and printed
  46. ?>
Copy code

This is test Output Control

  1. echo "
    this is test Output Control
    ";

  2. include_once 'cache/newFile.php';

  3. < p>$contents = ob_get_contents (); // Get the information stored in the buffer so far. The buffer only saves the content that will be output and printed to the page browser. PHP execution code will not be saved. // $contents = ob_get_clean( ); // Get the information stored in the buffer so far and close the clear buffer

  4. // ob_end_flush();//Output the information stored in the print buffer so far and close it Clear buffer

  5. ob_end_clean (); // Turn off clearing buffer contents

  6. file_put_contents ( $new_file, $contents ); // Write to file Content

  7. ?>

Copy code
2. Template file, template.html

  1. < ;html>
  2. %title%
  3. %title%


  4. %body%
  5. < /html>
Copy code

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles