Home > Backend Development > PHP Tutorial > A class written in PHP to generate static pages

A class written in PHP to generate static pages

WBOY
Release: 2016-07-25 09:05:49
Original
877 people have browsed it
  1. /**

  2. Function: Generate static pages
  3. Organized by: Scripting School bbs.it-home.org
  4. Date: 2013-2-16
  5. */
  6. class html
  7. {
  8. var $dir; //dir for the htmls(without/)
  9. var $rootdir; // root of html files(without/):html
  10. var $name; //HTML file storage path
  11. var $dirname; //Specified folder name
  12. var $url; //Get the source web page address of html file information
  13. var $time; //The time when filling in html file information
  14. var $dirtype; //Directory storage method: year, month,,,,,
  15. var $nametype; //HTML file naming method: name

  16. < ;p>function html($nametype='name',$dirtype='year',$rootdir='html')
  17. {
  18. $this->setvar($nametype,$dirtype,$rootdir);
  19. }< ;/p>
  20. function setvar($nametype='name',$dirtype='year',$rootdir='html')

  21. {
  22. $this->rootdir=$rootdir;
  23. $this- >dirtype=$dirtype;
  24. $this->nametype=$nametype;
  25. }

  26. function createdir($dir='')

  27. {
  28. $this->dir=$ dir?$dir:$this->dir;

  29. if (!is_dir($this->dir))

  30. {
  31. $temp = explode('/',$this- >dir);
  32. $cur_dir = '';
  33. for($i=0;$i
  34. {
  35. $cur_dir .= $temp[$i].'/';
  36. if (!is_dir($cur_dir))
  37. {
  38. @mkdir($cur_dir,0777);
  39. }
  40. }
  41. }
  42. }

  43. function getdir($dirname='',$ time=0)

  44. {
  45. $this->time=$time?$time:$this->time;
  46. $this->dirname=$dirname?$dirname:$this->dirname;< /p>
  47. switch($this->dirtype)

  48. {
  49. case 'name':
  50. if(empty($this->dirname))
  51. $this->dir=$this-> ;rootdir;
  52. else
  53. $this->dir=$this->rootdir.'/'.$this->dirname;
  54. break;
  55. case 'year':
  56. $this->dir=$this ->rootdir.'/'.date("Y",$this->time);
  57. break;

  58. case 'month':

  59. $this->dir=$ this->rootdir.'/'.date("Y-m",$this->time);
  60. break;

  61. case 'day':

  62. $this->dir= $this->rootdir.'/'.date("Y-m-d",$this->time);
  63. break;
  64. }

  65. $this->createdir();< ;/p>

  66. return $this->dir;

  67. }

  68. function geturlname($url='')

  69. {
  70. $this->url=$url ?$url:$this->url;

  71. $filename=basename($this->url);

  72. $filename=explode(".",$filename);
  73. return $filename[0];
  74. }

  75. function geturlquery($url='')

  76. {
  77. $this->url=$url?$url:$this->url;

  78. $durl=parse_url($this->url);

  79. $durl=explode("&",$durl[query]);
  80. foreach($durl as $surl)
  81. {
  82. $gurl=explode("=",$surl);
  83. $eurl[]=$gurl[1];
  84. }
  85. return join("_",$eurl);
  86. }

  87. < ;p>function getname($url='',$time=0,$dirname='')
  88. {
  89. $this->url=$url?$url:$this->url;
  90. $this- >dirname=$dirname?$dirname:$this->dirname;
  91. $this->time=$time?$time:$this->time;

  92. $this ->getdir();

  93. switch($this->nametype)

  94. {
  95. case 'name':
  96. $filename=$this->geturlname().'.htm ';
  97. $this->name=$this->dir.'/'.$filename;
  98. break;

  99. case 'time':

  100. $this->name= $this->dir.'/'.$this->time.'.htm';
  101. break;

  102. case 'query':

  103. $this->name=$ this->dir.'/'.$this->geturlquery().'.htm';
  104. break;

  105. case 'namequery':

  106. $this->name= $this->dir.'/'.$this->geturlname().'-'.$this->geturlquery().'.htm';
  107. break;

  108. $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->time.'.htm';
  109. break;

  110. }

  111. return $this->name;
  112. }

  113. function createhtml($url='',$time=0,$dirname='',$htmlname='')

  114. {
  115. $this->url=$url?$url:$this->url;
  116. $this->dirname=$dirname?$dirname:$this->dirname;
  117. $this->time=$time?$time:$this->time;
  118. //上面保证不重复地把变量赋予该类成员
  119. if(empty($htmlname))
  120. $this->getname();
  121. else
  122. $this->name=$dirname.'/'.$htmlname; //得到name
  123. $content=file($this->url) or die("Failed to open the url ".$this->url." !");;

  124. ///////////////关键步---用file读取$this->url

  125. $content=join("",$content);
  126. $fp=@fopen($this->name,"w") or die("Failed to open the file ".$this->name." !");
  127. if(@fwrite($fp,$content))
  128. return true;
  129. else
  130. return false;
  131. fclose($fp);
  132. }
  133. /////////////////以name为名字生成html
  134. function deletehtml($url='',$time=0,$dirname='')
  135. {
  136. $this->url=$url?$url:$this->url;
  137. $this->time=$time?$time:$this->time;

  138. $this->getname();

  139. if(@unlink($this->name))

  140. return true;
  141. else
  142. return false;
  143. }

  144. /**

  145. * function::deletedir()
  146. * Delete directory
  147. * @param $file directory name (without /)
  148. * @return
  149. */
  150. function deletedir($file)
  151. {
  152. if(file_exists($file))
  153. {
  154. if(is_dir($file))
  155. {
  156. $handle =opendir($file);
  157. while(false!==($filename=readdir($handle)))
  158. {
  159. if($filename!="."&&$filename!="..")
  160. $this->deletedir($file."/".$filename);
  161. }
  162. closedir($handle);
  163. rmdir($file);
  164. return true;
  165. }else{
  166. unlink($file);
  167. }
  168. }
  169. }
  170. }
  171. ?>

复制代码

您可能感兴趣的文章: php生成静态页面的三种方法与代码详解 php生成静态页面函数(php2html)的例子 php生成静态页面的方法(三个函数) 细说php生成静态文件之模板与缓存 虚拟主机上定时自动生成静态页面的方法 php生成静态文件的二种方法 php生成静态页面的详细教程 php生成静态html文件的原理分析 smarty生成静态页面的方法 了解php生成静态HTML文件的原理 PHP生成静态页面的方法 php生成静态html文件的三种方法



source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template