PHP code to publish news automatically

Source: Internet
Author: User
Tags key newsfile php code php script trim
News files are stored in a fixed directory in the format of the text file. txt, and the release of the news can be done automatically.

The process is made up of two parts:

The first is the display of news, implemented by shownews.php script, the code is as follows:
〈table border= "0" width= "90%"
〈?php
The first line in the news file puts the headline of the News
The news file must be a text file (. txt)
$newspath = "./news/"; Modify the directory where news files are stored
$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 = "〈a href="./pubnews.php?id= ". $a." "target=_blank〉". $s. " 〈/a〉 ";
Print $s. "N";
Print "(". Date ("Y year M month D-h:i:s", $newsfile [$key]).
n ";
print "〈/td〉〈/tr〉";
}
$HD-〉close ();
?〉
〈/table〉

Put the code where the news is displayed:
〈?php
Require "./shownews.php";
?〉

The second part is the release of news, implemented by pupnews.php script, the code is as follows:
〈?php
if ($id = = "")
{
Header ("Location:./shownews.php");
}
?〉
〈html〉
〈head〉
〈meta content= "Chenqiang" name=author〉
〈?php
$filename = "./news/". $id. ". TXT ";
$fa =file ($filename);
$n =count ($FA);
$s =trim ($fa [0]);
$s =htmlspecialchars ($s);
$t = "-News by WaterWall";
Print "〈title〉". $s. $t. " 〈/title〉n ";
?〉
〈/head〉
〈body〉
〈?php
Output text Caption
print "〈blockquote〉n";
Print "〈b〉〈center〉". $s. " n ";
Print "〈/center〉〈/b〉
〈p〉n ";
Output text body
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〉

The database can also be implemented, which is just the way the file is implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.