Home  >  Article  >  Backend Development  >  用文本文件实现的动态实时发布新闻的程序_php基础

用文本文件实现的动态实时发布新闻的程序_php基础

WBOY
WBOYOriginal
2016-05-17 09:45:51660browse

动态实时发布新闻的程序,可以根据发布时间,自动地排列显示顺序。新闻文件为存储在指定目录的文本文件组成,当有新闻的时候,程序会自动地将其显示出来,并且排列在页面的最开始部分。


News


News






  include('locationfilename.php');

  function createur1($text){
  //新闻的文本文件中这样插入你的链接
  //${http://mysite.ch}
  //或是${http://mysite.ch|My homepage in}
  //开始处理文本文件部分
    $s=$text;
    $a=strstr($s,'${');
    if ($a){
      $b=strstr($a,'}');
      if ($b){
        $la=strlen($a); $ls=strlen($s);
        $s=substr($s,0,$ls-$la);
        $a=substr($a,2);
        $lb=strlen($b); $la=strlen($a);
        $a=substr($a,0,$la-$lb); $b=substr($b,1);
        $ta=strstr($a,"|");
        if($ta){
          $la=strlen($a); $lt=strlen($ta);
          $linktext=substr($a,$la-$lt+1);
          $a=substr($a,0,$la-$lt);
        }
       else{
         $linktext=$a;
       }
      $s=$s."".$linktext."".$b;
      }
    }

  return($s);
  }

  //在这里修改你的新闻文件存放目录
  //切记,新闻文件必须是文本文件
  $newspath="/home/htdocs/test/new/";

  //设置数组
  $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)){
    $fa=file($newspath.$key);
    $n=count($fa);
    print "";
  }
  $hd->close();

?>

 
n";
    print "".date("d.m.Y - H:i:s",$newsfile[$key])."
n";
    for($i=0; $i      $s=chop($fa[$i]);
      $s=htmlspecialchars($s);
      $s=createur1($s);
      print $s."
n";
    }
    print "
Statement:
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