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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles