Home > php教程 > php手册 > body text

PHP fopen和fwrite函数实现创建html页面

WBOY
Release: 2016-06-13 10:44:19
Original
1822 people have browsed it

思路

用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面

index.html模板页面

{title}   文章内容为:{content}  index.php

  header('Content-Type:text/html; charset=utf-8');  $conn=mysql_connect('localhost','root','');  $db=mysql_select_db('bbs',$conn);  mysql_query('set names utf8');  $sql="select * from notice";  $query=mysql_query($sql);   //print_r($arr);  while($arr=mysql_fetch_array($query))   {       $title=$arr[title];       $content=$arr[content];       $file="index.html";       $neirong=$arr[id].".html";  $fp=fopen($file,'r');  $ht=fread($fp,filesize($file));  $str=str_replace('{title}',$title,$ht);  $str=str_replace('{content}',$content,$str);  fclose($file);  $file=fopen($neirong,'w');  $write=fwrite($file,$str);   }   ?> 

 

作者“PHP学习笔记”

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template