php自动生成新闻页,自动发布新闻页面的php代码

自动发布新闻页面的php代码

新闻文件以文本文件.txt的格式存放在一个固定的目录下,新闻的发布可以自动完成。

该过程由两部分组成:

第一是新闻的显示,由shownews.php脚本实现,代码如下:

〈tableborder="0"width="90%"〉

〈?php

//在新闻文件中的第一行放新闻的标题

//新闻文件必须是文本文件(.txt)

$newspath="./news/";//修改新闻文件存放的目录

$newsfile=array();

$hd=dir($newspath);

while($filename=$hd-〉read()){

$s=strtolower($filename);

if(strstr($s,".txt")){

$lastchanged=filemtime($newspath.$filename);

$newsfile[$filename]=$lastchanged;

}

}

arsort($newsfile);

for(reset($newsfile);$key=key($newsfile);next($newsfile)){

print"〈tr〉〈td〉n";

$fa=file($newspath.$key);

$s=trim($fa[0]);

$s=htmlspecialchars($s);

$lk=strlen($key);

$a=substr($key,0,$lk-4);

$s="〈ahref="./pubnews.php?id=".$a.""target=_blank〉".$s."〈/a〉";

print$s."n";

print"(".date("Y年m月d日-H:i:s",$newsfile[$key]).")

n";

print"〈/td〉〈/tr〉";

}

$hd-〉close();

?〉

〈/table〉

在显示新闻的地方放入代码:

〈?php

require"./shownews.php";

?〉

第二部分为新闻的发布,由pupnews.php脚本实现,代码如下:

〈?php

if($id=="")

{

Header("Location:./shownews.php");

}

?〉

〈html〉

〈head〉

〈metacontent="chenqiang"name=Author〉

〈?php

$filename="./news/".$id.".txt";

$fa=file($filename);

$n=count($fa);

$s=trim($fa[0]);

$s=htmlspecialchars($s);

$t="-newsbywaterwall";

print"〈title〉".$s.$t."〈/title〉n";

?〉

〈/head〉

〈body〉

〈?php

//输出文本标题

print"〈blockquote〉n";

print"〈b〉〈center〉".$s."n";

print"〈/center〉〈/b〉

〈p〉n";

//输出文本正文

for($i=1;$i〈$n;$i+=1)

{

$s=chop($fa[$i]);

$s=htmlspecialchars($s);

$s=trim($s);

print"".$s."

n";

}

print"〈/p〉〈/blockquote〉n";

?〉

〈/body〉

〈/html〉

用数据库也可实现,这只是文件形式的实现方式。

分享到:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值