站长论坛
标题:
长文章截取 longText
[打印本页]
作者:
tznktg
时间:
2007-9-27 22:24
标题:
长文章截取 longText
<?php
/*****************************************************
* Copyright :
www.phpyy.com
* FileName : long_text.inc.php
* Author : 朱武杰 <
phpyy@msn.com
>
* NickName : 纯粹误会 QQ:44922032
* Version : v1.0
* Description : 长文章截取 longText
* CreatDate : 2005-11-6
* HistoryEdit : 历史修改记录
---------- 第一次修改 -------------------
Author : 朱武杰
Date : 2005-11-8
Summary :
-------------- ------------------------
---------- 第二次修改 -------------------
Author : 朱武杰
Date : 2005-11-11
Summary : 加入XML输出
-------------- ------------------------
*****************************************************/
class longText{
/****
GB2312 0xB0 - 0xF7 0xA0 - 0xFE
GBK 0x81 - 0xFE 0x40 - 0xFE
GB18030 的双字节 0x81 - 0xFE 0x40 - 0x7E, 0x80 - 0xFE
GB18030 的四字节 0x81 - 0xFE 0x30 - 0x39 0x81 - 0xFE 0x30 - 0x39
***/
var $m_encoding ='gbk'; //encoding charset
var $m_text =''; //content
var $m_size =''; //char/page
var $m_mid =''; //middle link number/page
var $m_page =''; //currently page number
var $r_text =''; //return content
var $r_author =''; //return author
var $r_title =''; //return title
var $r_prev =''; //prev page link
var $r_next =''; //next page link
var $r_first =''; //first page link
var $r_last =''; //last page link
var $r_midd =''; //middle page link
var $r_html =''; // return html code;
var $r_xml =''; // return xml code;
//构造HTML风格代码并输出
function htmlConstruct(){
$this-> explodeText();
$this-> textPage ();
$this-> htmlHttp();
$this-> printHtmlCode ();
}
//构造XML风格代码并输出
function xmlConstruct (){
$this-> explodeText();
$this-> textPage ();
$this-> xmlHttp ();
$this-> printXmlCode ();
}
//连接HTML风格代码
function htmlHttp (){
$this-> r_html=
$this->r_first
.$this->r_prev
.$this->r_midd
.$this->r_next
.$this->r_last
."<br/>\n"
."《".$this->r_title."》"
."\n"
.$this->r_author
."<br/>"
."<br/>"
.$this->r_text;
}
//连接XML风格代码
function xmlHttp (){
$this->r_xml=''
."<code>"
."<title>".$this->r_title."</title>\n"
."<author>".$this->r_author."</author>\n"
."<fist>".$this->r_first."</fist>\n"
."<prev>".$this->r_prev."</prev>\n"
."<midd>".$this->r_midd."</midd>\n"
."<next>".$this->r_next."</next>\n"
."<last>".$this->r_last."</last>\n"
."<br/>"
."<text>".$this->r_text."</text>"
."</code>";
}
//输出HTML风格代码
function printHtmlCode (){
echo $this->r_html;
}
//输出XML风格代码
function printXmlCode (){
header('Content-type: text/xml');
echo'<?xml version="1.0" encoding="GB2312"?>';
echo $this->r_xml;
}
//构造类
function longText ($type='xml',$text,$size='200',$mid=5,$encoding='gbk',$title='',$author=''){
if(!function_exists('mb_substr')){
die ('请添加mb(Multi-Byte String Functions)扩展');
}
$this->r_title =htmlspecialchars ($title);
$this->r_author =htmlspecialchars ($author);
$this->m_text =strip_tags($text);
$this->m_text =str_replace(" "," ",$this->m_text);
$this->m_text =str_replace("\r","",$this->m_text);
$this->m_text =str_replace("\n\n","\n",$this->m_text);
$this->m_size =$size+0;
$this->m_mid =$mid+0;
$this->m_encoding =$encoding;
$this->m_page =@$_GET['page']+0;
$this->m_start =$this->m_page * $this->m_size;//start'th position in string
$this->m_textLen =mb_strLen($this->m_text,$this->m_encoding);//the length of string
$this->m_pages =floor($this->m_textLen/$this->m_size);//pages
switch($type){
case 'html':
$this->htmlConstruct();
break;
case 'xml':
$this->xmlConstruct();
break;
default:
$this-> explodeText();
$this-> textPage ();
}
}
//截取代码
function explodeText (){
$this->r_text=nl2br( mb_substr($this->m_text ,$this->m_start,$this->m_size,$this->m_encoding) );
}
//分页
function textPage (){
$this->r_prev =$this->m_page>0?" <a href=\"?page=".( $this->m_page-1)."\"><<</a> ":" << ";
$this->r_next =$this->m_page<$this->m_pages ?" <a href=\"?page=".( $this->m_page+1)."\">>></a> ":" >> ";
$this->r_first =$this->m_page>0?" <a href=\"?page=".(0)."\">|<<</a>":"|<<";
$this->r_last =$this->m_page<$this->m_pages ?" <a href=\"?page=".( $this->m_pages )."\">>>|</a> ":">>| ";
$begin=$this->m_page-ceil($this->m_mid/2);
if($begin<0){
$begin=0;
}
$end=$this->m_page+$this->m_mid-ceil($this->m_mid/2);
if($end>$this->m_pages){
$end=$this->m_pages;
}
for($i=$begin;$i<$end;$i++){
$this->r_midd.=$i==$this->m_page?" ".($i+1)." ":" <a href=\"?page=$i\">".($i+1)."</a> ";
}
}
}
////////////// XML test (XML 测试) //////////////////////
///*
error_reporting(2047);
$filename="第一次亲密接触.txt";
$size=200;
$mid=5;
$text=file_Get_contents($filename);
$obj = new longText ('xml',$text,$size,$mid,'gbk','第一次亲密接触','痞子蔡');
//*/
////////////// HTML test (HTML 测试) //////////////////////
/*
error_reporting(2047);
$filename="第一次亲密接触.txt";
$size=200;
$mid=5;
$text=file_Get_contents($filename);
$r=array_sum(explode(" ",microtime()));
$obj = new longText ('html',$text,$size,$mid,'gbk','第一次亲密接触','痞子蔡');
$r=(array_sum(explode(" ",microtime()))-$r) ." 秒 ";
echo "<br/>".$r;
*/
?>
作者:
tznktg
时间:
2007-9-27 22:24
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//File: main_news.php
//Author: TZNKTG
//Purpose: This page is display news of new
///////////////////////////////////////////////////////////////////////////////////////////////////////////
$lastest_news_sql = "select id,sortid,bsortid,title,content,datetime from ".$article_table." order by id desc limit 0,1";
$db->execute($lastest_news_sql);
while($newsinfo=$db->reader())
{
$id = $newsinfo['id'];
$ssid = $newsinfo['sortid'];
$bsid = $newsinfo['bsortid'];
$title = $newsinfo['title'];
$content = $newsinfo['content'];
$datetime = $newsinfo['datetime'];
$dts = split(" ",$datetime);
$ydms = split("-",$dts[0]);
$newsydm = $ydms[0]."-".$ydms[1]."-".$ydms[2];
$ydm = $ydms[0]."年".$ydms[1]."月".$ydms[2]."日";
}
$par = "id=".$bsid."&ssid=".$ssid."&nid=".$id;
$content = str_replace(" "," ",cnsubstr($content,280));
$content = str_replace("\r\n\r\n","<br>",$content);
$content = str_replace("\r\n\n","<br>",$content);
$content = str_replace("\r\n","<br>",$content);
$content = str_replace("\\n","<br>",$content);
$content = str_replace("\n","<br>",$content);
?>
<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tbody>
<tr>
<td width="5"><img width="5" border="0" height="5" src="images/main_top_left.gif" ></td>
<td width="100%" background="images/main_top_center.gif" height="5"></td>
<td width="5"><img width="5" border="0" height="5" src="images/main_top_right.gif" ></td>
</tr>
</tbody>
</table>
<table class="main_bg" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tbody>
<tr>
<td width="252">
<table cellspacing="0" cellpadding="0" width="252" border="0">
<tbody>
<tr>
<td colspan="3"><img height="10" src="images/pic01.gif" width="250"></td>
</tr>
<tr>
<td width="19"><img height="161" src="images/pic02.gif" width="15"></td>
<td><a id="imagepart1_hlimg" onclick="javascript:pictarget();return false;" href="javascript:void(null)"><img id="imagepart1_img" style="filter: revealtrans(duration=2.0,transition=12); " src="<? echo $new_news_img ?>" height="150" width="218" border="0" name="img"> </a><img height="11" src="images/pic03.gif" width="218">
</td>
<td width="15"><img height=161 src="images/pic04.gif" width="19"></td>
</tr>
<tr>
<td valign="top" align="middle" colspan="3"><a
style="font-weight: bold; filter: dropshadow(color=#ffccff,offx=-1,offy=-1,positive=1); color: #0099ff; font-family: verdana,'宋体'; position: relative; height: 1px; text-decoration: none"
href="javascript:void(null)"><span id="imagepart1_lbltitle"></span></a>
</td>
</tr>
</tbody>
</table>
</td>
<td valign="top">
<table height="190" cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td height="20"> </td>
</tr>
<tr>
<td valign="top">
<a href="shownews.php?<? echo $par ?>" title="<? echo $title."\n日期:".$ydm ?>" target="_blank"><b><? echo $title?></b></a><span class="style1"> (<? echo $newsydm ?>)</span>
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td align="left" height="10"></td>
</tr>
<tr>
<td align="left" height="18" class="t18m"><? echo $content ?></td>
</tr>
</tbody>
</table>
</td>
<td width="5"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tbody>
<tr>
<td width="5"><img width="5" border="0" height="5" src="images/main_bot_left.gif"></td>
<td width="100%" height="5" background="images/main_bot_center.gif"></td>
<td width="5"><img width="5" border="0" height="5" src="images/main_bot_right.gif"></td>
</tr>
</tbody>
</table>
function cnsubstr($string,$sublen)
{
if($sublen>=strlen($string))
{
return $string;
}
$s="";
for($i=0;$i<$sublen;$i++)
{
if(ord($string{$i})>127)
{
$s.=$string{$i}.$string{++$i};
continue;
}else{
$s.=$string{$i};
continue;
}
}
if(strlen($string)>$sublen) $s.="...";
return $s;
}
欢迎光临 站长论坛 (https://www.tzlink.com/bbs/)
Powered by Discuz! X3.2