发信人: liumx() 
整理人: dalasthunder(2002-07-25 06:09:10), 站内信件
 | 
 
 
【 在 hfzdh () 的大作中提到: 】
 
 呵..我有一个.不过要特定格式标记出主题,时间,内容
 你可以参考看看..
 #! /usr/local/bin/php
 <?
 if(empty($argv[1]) || empty($argv[2]) || empty($argv[3])){
 	echo "Useage: last.php <infile.name> <filter> <subcat> [outfile]";
 	exit();
 }else{
 	$infile=$argv[1];
 	$filter=$argv[2];
 	$subcat=$argv[3];
 	if(!file_exists($infile)){
 		echo "source File not exists .";
 		exit();
 	}
 	if(intval($filter)<=0 || intval($filter)>12){
 		echo "Invalid filter number:$filter";
 		exit();
 	}
 	if(intval($subcat)<=0 || intval($subcat)>=10){
 		echo "Invalid subcat number: $subcat";
 		exit();
 	}
 }
 
 $file=file($infile);
 function strip($line){
 	$tostrip=array(
 		'在线词典',
 		'MyINFOBANK',
 		"将此文章收入.",
 		'版权保护说明',
 		"本正文被点击次数为 [0-9]",
 		"--",
 	//	'正文显示:',
 		'牋',
 	//	\f\r\t\n]
 	);
 	while(list($key,$value)=each($tostrip)){
 		$line=ltrim(ereg_replace("[\f\t][0-d]","",$line));
 		if(ereg("$value",$line,$result)){
 			$line=ereg_replace("$value","",$line);
 		}			
 	}
 	return $line;
 }
 //end of strip funciton
 
 function replace($line){
 	$toreplace=array(
 	'【行业分类】'=>"【",  
 	'【地区分类】'=>"【",
 	'【时间分类】'=>"【",
 	'【文献出处】'=>"【",
 	'【标  题】'=>"【",
 	'【副 标 题】'=>"【",
 	'【正  文】'=>"【",
 
 	//'【颁文机构】'=>"【",
 	);
 	while(list($key,$value)=each($toreplace)){
 		if(ereg($key,$line,$result)){
 			$line=ereg_replace($key,$value,$line);
 		}
 	}
 	return $line;
 }
 //end or replace function
 
 
 //数据库操作开始
 include("dbaccess.php");
 $dbh=db_connect();
 function insert($author,$source,$subject,$content){
 	global $dbh,$filter,$subcat,$key;
 	if(!$dbh){
 		echo "Unable to connct to server";
 		exit();
 	}
 	if($filter<=0 || $subcat<=0){
 		echo "error,invalid filter or subcat number";
 		exit();
 	}
 	if(empty($subject)||empty($content)){
 		echo "Error:Empty subject or content";
 		exit();
 	}
 	/*
 	$author=$field[6];	//来自
 	$source=$field[4];	//来自
 	$subject=$field[5];	// 来自
 	$content=$field[7]	;//来自
 	
 	echo $key,":",$author,$source,$subejct,"\n";
 	return;*/
 	$sql_get_id="select currentval from counter where tbname='news_main'" 
 ;
 	$getidresult=@mysql_query($sql_get_id);
 	$currentidarray=@mysql_fetch_array($getidresult);
 	$currentid=$currentidarray[currentval];
 	$length=4;
 	$serial=strval($currentid);
 	for($i=0;$i<$length-strlen($currentid);$i++){
 	  $serial='0'.$serial;
 	}
 	if(empty($serial)){
 		echo "unable to get serial";
 		exit();
 	}
 	$date=GetDate();
 	strlen($month)==1?$month='0'.$month:'';
 	strlen($day)==1?$day='0'.$month:'';
 	$code=$year.$month.$day.strval($serial);
 	$pubdate=date("Y-m-d");
 	$updateby="mark";
 	$class=1;
 	$pov=365;
 	$template="template1.php";
 	$comment="";
 	$date=GetDate();
 	$year=$date["year"];
 	$month=$date["mon"];
 	$day=$date["day"];
 	strlen($month)==1?$month='0'.$month:'';
 	strlen($day)==1?$day='0'.$month:'';
 	
 	$code=$year.$month.$day.strval($serial);
 	
 	//News Code Define complete..
 	if($image=='') $image='N';
 	if($html1=='') $html1='N';
 	if($html2=='') $html2='N';
 	$subject=addslashes($subject);
 	$content=addslashes($content);
 	if($subject==''||$content==''){
 		echo "NO subject or content .error";
 		exit();
 	}
 	$sql_new="insert into 	news_main(id,code,author,source,subject,catid, 
 subcatid,pubdate,inputdate,updateby,class,pov,content,template,image,h 
 tml1,html2,comment) values(0,'$code','$author','$source','$subject',$f 
 ilter,$subcat,'$pubdate',now(),'$updateby',$class,$pov,'$content','$te 
 mplate','$image','$html1','$html2','$comment')";
 
 	$sth=mysql_query($sql_new);
 	if($sth<>1){
 		$errmsg="Unable to Execute Query:Please contact administrator!ErrorC 
 ode 3\n";
 		echo $errmsg;
 		echo "<br>",$sql_new;
 		exit();
 	}else{
 		$dbh=mysql_query("update counter set currentval=currentval+1 where t 
 bname='news_main'");
 		if($dbh<>1){
 			$errmsg="Error,Unable to Update Counter";
 			echo $errmsg;
 			exit();
 		}
 		echo "insert ok-$key \n";
 	}
 
 }
 //end of insert function;
 
 while(list($key,$value)=each($file)){
 	$file[$key]=ltrim(strip($file[$key]));
 	$file[$key]=replace($file[$key]);
 }
 reset($file);
 
 $data=array("file",);
 $i=0;	
 while(list($key,$value)=each($file)){
 	if(ereg("正文显示:",$value,$result)){	//一篇文章开始
 		$i++;
 		$data[$i].="\r\n";
 	}else{
 		$data[$i].=$value;	//将所有数据放入数组
 	}
 }
 
 while(list($key,$doc)=each($data)){
 	if ($key==0){ 
 		continue;
 	}
 /*	【行业分类】投资 1
 	【颁文机构】山西省政府 2
 	【时间分类】20000930 3
 	【文献出处】大同日报 4
 	【标 题】国内经济实体投资的若干规定(4561字) 5
 	【正 文】
 **************************
 	'【行业分类】'=>"【",  
 	'【地区分类】'=>"【",
 	'【时间分类】'=>"【",
 	'【文献出处】'=>"【",
 	'【标  题】'=>"【",
 	'【副 标 题】'=>"【",
 	'【正  文】'=>"【",
 	6*/
 		
 	
 	$field=split('【',$doc);
 	$source=$field[4];
 	$subject=$field[5];
 
 	if(empty($field[7])){
 		$content=$field[6];
 	}else{
 		$author=$field[6];
 		$content=$field[7];
 	}
 
 	if(empty($subject) || $subject=='' || empty($content) || $content=='' 
 ){
 		echo "What's wrong with $key ? empty subject" ;
 		continue;
 	}else{
 	//	echo "$key:A-S-S-C",$author,$source,$subject;
 		insert($author,$source,$subject,$content);
 	//	echo "excute insert $key \n";
 	}
 }
 ?>
 
 
 
 
 
 -- ※ 修改:.liumx 于 Oct 16 11:08:22 修改本文.[FROM: 211.100.0.28] ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 211.100.0.28]
  | 
 
 
 |