Analyze the code example for generating html files in PHP

Source: Internet
Author: User

LearningPHP generates html file 1. Here is a template method!

  1. <? Php
  2. $ Fp = fopen ("templets.html", "");
  3. If ($ fp ){
  4. $ Fup = fread ($ fp, filesize
    ("Templets.html "));
  5. $ Fp2 = fopen ("html.shtml", "w ");
  6. If ($ fwrite ($ fp2, $ fup )){
  7. $ Fclose ($ fp );
  8. $ Fcolse ($ fp2 );
  9. Die ("template written successfully ");
  10. } Else {
  11. Fclose ($ fp );
  12. Die ("failed to Write template! ");
  13. }
  14. }
  15. ?>
 
 
  1. <? Php
  2. $ Fp = fopen ("templets.html", "");
  3. If ($ fp)
  4. {$ Fup = fread ($ fp, filesize ("templets.html "));
  5. $ Fp2 = fopen ("html.shtml", "w ");
  6. If ($ fwrite ($ fp2, $ fup ))
  7. {$ Fclose ($ fp );
  8. $ Fcolse ($ fp2 );
  9. Die ("template written successfully ");}
  10. Else {fclose ($ fp );
  11. Die ("failed to Write template! ");
  12. }
  13. }
  14. ?>

Simply write the Template into a file and save it as HTML.

PHP generates html file 2 and generates html file names by Time

 
 
  1. <? Php
  2. $ Content = "this is a file name based on date and time.
    Static generation of Web Page test files, file name format 1
    <Font color = # ff0000> year, month, day, hour, minute, and second
    . Html </font> ";
  3. $ Datedate = date ('ymdhis ');
  4. $ Fp = fopen (date ('ymdhis '). '.html', "w ");
    // This function can be used to open local or remote files 'W'
    The open file mode is write, and the file Pointer Points to the beginning,
    Set the length of the original file to 0. If the file does not exist, create a new file.
  5. If (fwrite ($ fp, $ content )){
    // The format is. int fwrite (int fp (file name ),
    String (content), int [length]
    (Length); this function writes string to the file
    Data Flow pointer fp. If the length is specified,
    The specified length string is written, or the end of the string is written.
  6. Fclose ($ fp); // The function is used to close opened files.
    The pointer fp. True is returned for success, and false is returned for failure.
  7. Die ("template written successfully ");
  8. }
  9. Else {
  10. Fclose ($ fp );
  11. Die ("failed to Write template! ");
  12. }
  13. Echo ($ content );
  14. ?>

 
 
  1. <? Php $ content = "this is a file name based on date and time.
    Static generate a test file for the web page, the file name format is generally
  2. <Font color = # ff0000> YYYY. html </font> ";
  3. $ Datedate = date ('ymdhis ');
  4. $ Fp = fopen (date ('ymdhis '). '.html', "w ");
  5. // This function can be used to open local or remote files 'W'
    The open file mode is write, and the file Pointer Points to the beginning,
    Set the length of the original file to 0. If the file does not exist, create a new file.
  6. If (fwrite ($ fp, $ content ))
  7. {// The format is. int fwrite (int fp (file name ),
    String (content), int [length] (length ));
  8. This function writes the string to the fp pointer of the file data stream.
    If the length is specified, the specified length string is written,
    Or write to the string to end. Fclose ($ fp );
  9. // Function is used to close the fp pointer of an opened file.
    True is returned for success, and false is returned for failure.
  10. Die ("template written successfully ");}
  11. Else {fclose ($ fp); die ("failed to Write template! ");
  12. }
  13. Echo ($ content);?>
  14.  

PHP generates html file 3. The following is a method for converting file names.

 
 
  1. < ?php   
  2. $s_fname = "93e.php";   
  3. $o_fname = "93e.htm";   
  4. ob_end_clean();   
  5. ob_start();   
  6. include($s_fname);   
  7. $length = ob_get_length();   
  8. $buffer = ob_get_contents();   
  9. $buffer = eregi_replace("r","",$buffer);   
  10. ob_end_clean();   
  11. $fp = fopen($o_fname,"w+");   
  12. fwrite($fp,$buffer);   
  13. fclose($fp);   
  14. ?>  


 

 
 
  1. < ?php 
  2. $s_fname = "93e.php"; 
  3. $o_fname = "93e.htm"; 
  4. ob_end_clean(); ob_start(); 
  5. include($s_fname); 
  6. $length = ob_get_length(); 
  7. $buffer = ob_get_contents(); 
  8. $buffer = eregi_replace("r","",$buffer); 
    ob_end_clean(); 
  9. $fp = fopen($o_fname,"w+"); 
    fwrite($fp,$buffer); fclose($fp); 
  10. ?>  

In this way, 93e. php is converted to a static HTML file. Note that the files to be converted cannot contain ob_end_clean (); and ob_start (); statements, and the directory must have the write permission.

The above describes how to generate html files in PHP for your reference only.


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.