Maison > php教程 > php手册 > le corps du texte

php添加文章时生成静态HTML文章的实现代码

WBOY
Libérer: 2016-06-06 20:32:24
original
1008 Les gens l'ont consulté

PHP生成静态文章HTML,有批量的生成,但比较标准的应该是在添加文章时就生成HTML文章,编辑时再重新生成HTML文章,删除文章时同样也样删除多余出来的HTML文章,这时批量生成就显得有点力不从心了,下面就介绍一下PHP在添加文章时如何生成静态的HTML文件

PHP生成静态文章HTML,有批量的生成,但比较标准的应该是在添加文章时就生成HTML文章,编辑时再重新生成HTML文章,删除文章时同样也样删除多余出来的HTML文章,这时批量生成就显得有点力不从心了,下面就介绍一下PHP在添加文章时如何生成静态的HTML文件。

简单的添加文章表单这里就不写了,下面的这些源码是接受表单传过来的值而执行的程序源码,可以先拿过去测试一下。。。
代码如下:
ob_start();
require_once("../inc/conn.php");
$typ=$_POST["typ"];
$title=$_POST["title"];
$content=$_POST["d_content"];
$author=$_POST["author"];
$source=$_POST["source"];
$mobanpath="../moban/moban.html";
if(file_exists($mobanpath))
{
$fp=fopen($mobanpath,"r");
$str=fread($fp,filesize($mobanpath));
$str=str_replace("-title-",$title,$str);
$str=str_replace("-time-",date("Y-m-d H:i:s"),$str);
$str=str_replace("-content-",$content,$str);
$str=str_replace("-author-",$author,$str);
$str=str_replace("-source-",$source,$str);
$foldername=date("Y-m-d");
$folderpath="../newslist/".$foldername;
if(!file_exists($folderpath))
{
mkdir($folderpath);
}
$filename=date("H-i-s").".html";
$filepath="$folderpath/$filename";
if(!file_exists($filepath))
{
$fp=fopen($filepath,"w");
fputs($fp,$str);
fclose($fp);
}
$filepath=$foldername."/".$filename;
$sql="insert into newscontent (newstypeid,newstitle,newspath,newssource,newstime) values ($typ,'$title','$filepath','$source','".date("Y-m-d H:i:s")."')";
mysql_query($sql);
header("location:add.php");
}
?>

ob_start()是开启session的意思,写不写关系不是很大,这里按照PHP标准的写法添加上去了。

第二句就是包含链接数据库的文件了。

下面$内容=$_POST["内容"];就是接受过来表单的内容了。有几项就接受几项吧。

$mobanpath="../moban/moban.html"; 这个是模板的路径。

if(file_exists($mobanpath)):检验模板的文件是否存在,如果存在的话就执行下面的模板标签替换操作。

再往下就是利用str_replace来执行模板标签的替换操作了,同时建立HTML文件,最后通过SQL语句添加到数据库里面,再返回到add.php添加文章标单的地方,这里的生成HTML规则可以自己添加,比如按照时间来生成,或者按照文章ID来生成等。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!