Php static page generation methods

Source: Internet
Author: User
Php static page generation methods

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

  2. // ReplaceTemplateString is used to replace the specified string in the template.
  3. Function replaceTemplateString ($ templateString ){
  4. // The variable to replace
  5. $ Title = "article title ";
  6. $ Body = "this is the subject of the article ";
  7. // Replace the specified string in the template
  8. $ ShowString = str_replace ("% title %", $ title, $ templateString );
  9. $ ShowString = str_replace ("% body %", $ body, $ showString );
  10. // Return the replaced 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: obtain the overall template content string, replace the string, 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 to the generated HTML file.

  23. /*

  24. // Method 2: cyclically read the content strings in each line of the template. replace the strings and add them to the new file in sequence.
  25. While (! Feof ($ template_juBing) {// feof () function checks whether the object has reached the end. Returns TRUE if the file pointer ends or an error occurs. Otherwise, FALSE (including socket timeout and other cases) is returned ).
  26. $ TemplateString = fgets ($ template_juBing); // fgets (file, length) reads a row from the file pointer and returns a string with a maximum length of length-1 bytes, including line breaks. If length is not specified, the default value is 1 K, or 1024 bytes.
  27. $ ShowString = replaceTemplateString ($ templateString );
  28. Fwrite ($ newFile_juBing, $ showString); // when you write content to the opened pointer file for the first time, the original content in the pointer file will be replaced. before the pointer is closed, the fwrite function adds content after the added content
  29. }
  30. */
  31. // Close the file pointer
  32. Fclose ($ newFile_juBing );
  33. Fclose ($ template_juBing );

  34. /*

  35. Relationship between databases and static pages
  36. Generally, after a piece of information is added to the database, a static page of the information is generated. Therefore, it is best to add a field in the database table to store the path file name corresponding to the static page for later modification and deletion.

  37. Template replacement

  38. Generally, if you need to modify the static HTML page template, you can delete all generated HTML pages and recreate them. (Or overwrite all to generate)

  39. Dynamic operations on static pages

  40. Sometimes, some dynamic operations are also required on the created static HTML page. For example, in a news system, the click rate of each piece of news is counted.
  41. You can use an image control with a width and height of 0 pixels to hide and call a php page to implement the page counter function, as shown in figure
  42. Link to the static page of the Directory

  43. Generally, for systems that use static pages, the directory pages of the connected list are also generated as static HTML files for visitors to browse.
  44. Note that each increase or decrease of a database information will affect the link list. Therefore, you must update the static page of the link directory every time you add or delete the database information.
  45. The paging design can be completed by creating static pages of multiple linked directories.
  46. */

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

  48. Ob_start (); // when the buffer zone is activated and there is ob_end_clean (), all output non-file header information will not be printed to the page, but saved in the internal buffer zone. If no ob_end_clean () exists, the information is printed both by the internal buffer and output.
  49. ?>

This is test Output Control

  1. Echo"
    This is test Output Control
    ";

  2. Include_once 'cache/newFile. php ';

  3. $ Contents = ob_get_contents (); // obtain the information stored so far in the buffer. the buffer only saves the printed content that will be output to the browser on the page. php code execution will not be saved.

  4. // $ Contents = ob_get_clean (); // obtain the information stored so far in the buffer and disable clearing the buffer.

  5. // Ob_end_flush (); // output the information stored so far in the buffer and disable clearing the buffer.

  6. Ob_end_clean (); // Close the buffer clearing content

  7. File_put_contents ($ new_file, $ contents); // write content to the file

  8. ?>

2. template.html

  1. % Title %
  2. % Title %
  3. %body%

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.