分析PHP生成html文件的具体代码示例

开发 后端
在这里,我们为大家介绍了三种PHP生成html文件的实现方法,希望对有需要的朋友有所帮助,提高大家对PHP语言的理解程度。

学习PHP语言的程序员们都知道,PHP是一个功能强大的嵌入式HTML脚本语言,被许多程序员们用来创建网站。我们今天要为大家介绍的就是PHP生成html文件的相关实现方法。

#t#PHP生成html文件1,下面使用模版的一个方法!

  1. < ?php   
  2. $fp = fopen ("templets.html","a");   
  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 ("写入模板成功");   
  10. } else {   
  11. fclose ($fp);   
  12. die ("写入模板失败!");   
  13. }   
  14. }   
  15. ?>   
  1. < ?php   
  2. $fp = fopen ("templets.html","a");   
  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 ("写入模板成功"); }   
  10. else { fclose ($fp);   
  11. die ("写入模板失败!");   
  12. }   
  13. }   
  14. ?>  

简单的将模板写进一个文件中存为html.html

PHP生成html文件2,按时间生成html文件名

  1. < ?php   
  2. $content = "这是一个以日期时间为文件名
    的静态生成网页的测试文件,文件名格式一
    般为< font color=#ff0000>年月日时分秒
    .html< /font>"
    ;   
  3. $datedate = date('YmdHis');   
  4. $fp = fopen (date('YmdHis') . '.html',"w");
    //本函数可用来打开本地或者远端的文件 'w' 
    开文件方式为写入,文件指针指到开始处,
    并将原文件的长度设为 0。若文件不存在,则建立新文件。   
  5. if (fwrite ($fp,$content)){
    //格式是.int fwrite(int fp(文件名),
     string string(内容), int [length]
    (长度));本函数将字符串 string 写入文件
    资料流的指针 fp 上。若有指定长度 length,
    则会写入指定长度字符串,或是写到字符串结束。   
  6. fclose ($fp);//函数用来关闭已经打开的文
    件的指针 fp。成功返回 true,失败则返回 false。   
  7. die ("写入模板成功");   
  8. }   
  9. else {   
  10. fclose ($fp);   
  11. die ("写入模板失败!");   
  12. }   
  13. echo ($content);   
  14. ?>  

 

 

  1. < ?php $content = "这是一个以日期时间为文件名
    的静态生成网页的测试文件,文件名格式一般为
  2. < font color=#ff0000>年月日时分秒.html< /font>";
  3.  $datedate = date('YmdHis'); 
  4. $fp = fopen (date('YmdHis') . '.html',"w");
  5. //本函数可用来打开本地或者远端的文件 'w' 
    开文件方式为写入,文件指针指到开始处,
    并将原文件的长度设为 0。若文件不存在,则建立新文件。 
  6. if (fwrite ($fp,$content))
  7. {//格式是.int fwrite(int fp(文件名),
     string string(内容), int [length](长度));
  8. 本函数将字符串 string 写入文件资料流的指针 fp 上。
    若有指定长度 length,则会写入指定长度字符串,
    或是写到字符串结束。 fclose ($fp);
  9. //函数用来关闭已经打开的文件的指针 fp。
    成功返回 true,失败则返回 false。 
  10. die ("写入模板成功"); } 
  11. else { fclose ($fp); die ("写入模板失败!"); 
  12. echo ($content); ?> 
  13.  

 

PHP生成html文件3,下面为转换文件名的一个方法

  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. ?>  

这样就可以把93e.php转化为静态的HTML文件了,要注意的是待转换的文件里不能有,ob_end_clean();和 ob_start();语句,且目录要有写权限。

以上就是PHP生成html文件的实现方法介绍,仅供大家参考学习。

责任编辑:曹凯 来源: 百度博客
相关推荐

2009-11-30 17:02:13

PHP函数preg_m

2009-11-24 13:33:49

2009-11-16 10:57:51

PHP上传文件代码

2009-11-24 13:26:17

2009-11-24 14:52:45

PHP动态多文件上传

2009-11-30 09:35:15

PHP递归算法

2009-11-27 16:07:10

2009-11-27 16:47:36

PHP无限分类

2009-11-24 18:37:55

PHP数组转换

2009-11-24 15:01:59

PHP通用文件上传类

2009-12-07 15:41:51

PHP图片加水印

2009-11-27 15:13:00

PHP静态变量stat

2011-08-15 09:47:49

PHP

2009-12-04 17:06:47

PHP读取Excel文

2009-11-23 13:00:40

PHP获取QQ邮箱好友

2009-11-25 10:31:35

PHP数组实现单链表

2009-12-02 10:49:59

PHP解析XML元素结

2009-11-24 18:23:26

PHP函数array_

2009-11-26 14:23:10

PHP正则模式修正符

2012-04-19 17:16:32

Titanium实例代码分析
点赞
收藏

51CTO技术栈公众号