這個類並不難,思路來自分頁,思想是在內容裡加入不在頁面顯示的標籤, 作為分頁的標誌!
類中有個url分析的函數,是親自實驗出來的!可能跟您想要的不一樣!主要還有個在後台使用的js,那是個痛點,我用了半天的時間,調試跟尋找
<?php
/**
author sanshi
QQ:35047205
Email:sanshi0815@tom.com
MSN:sanshi0815@tom.com
*
*@version 1.0.0 2005/10/25
*
*@deprecated
長文章分頁
edit 2005/10/26 add function is _delNull()
*/
class stringPage
{
var $sumpage = 0;//看此篇文章有多少頁
var $showPage = 0;//要顯示那段
var $curPage = 0;//當前顯示段
var $content = array();//內容儲存數組
var $file; //設定傳輸頁
var $pvar;
var $format; //設定分隔字元號
/*
param $text 要分隔的文本
param $format 分割符號
*/
function stringPage($text,$format='<!--@-->')
{
$this->content = $this->_delNull( explode($format,$text) );
//去掉空元素,並去掉相同的元素
//$this->content = $this->_delNull( array_unique( explode($format,$text) ) );
$this->sumpage = count($this->content);
$this->format = $format;
}
function _delNull($array)
{
$temp=array();
$f="/^( |<p>| ){0,}$/";
for($i=0;$i<count($array);$i++)
{
if(!preg_match_all($f,$array[$i],$out))
{
$temp[]=$array[$i];
}
}
return $temp;
}
function show($file='',$pvar='')
{
$this->file = empty($file)? $HTTP_SERVER_VARS['PHP_SELF'] : $file ;
$this->pvar = empty($pvar)? 'apage' : $pvar;
$p = $_GET[$this->pvar];
$p = ( $p=='')? 1 : $p;
$this->curPage = $p;
$tmp='';
//$tmp.= $this->_showHead();
$tmp.= $this->content[$this->curPage-1];
$tmp.= $this->_showFoot();
return $tmp;
//echo $this->_showFoot();
}
function _makeUrl($url)
{
$arrayUrl=parse_url($url);
$q=$arrayUrl['query'];
if(strpos($q,$this->pvar)===false)
{
return $url.'&'.$this->pvar."=";
}else{
$url=explode('=',$q);
$url[count($url)-1]='';
//$arrayUrl['query']=implode('=',$url);
return $this->file.'?'.implode('=',$url);
}
}
function _showHead()
{
$url=$this->_makeUrl($_SERVER["REQUEST_URI"]);
$upPage=$this->curPage-1;
$downPage=$this->curPage+1;
$head="<dir class='article_bar'>";
if($this->curPage>1&&$this->curPage>0)
{
$head.= "<a href=".$url.$upPage.">上一頁</a> ";
}
if($this->sumpage>1&&$this->curPage<$this->sumpage)
{
$head.= "<a href=".$url.$downPage.">下一頁</a> ";
}
$head.="第{$this->curPage}頁 共{$this->sumpage}頁";
$head.="</dir>";
return $head;
}
function _showFoot()
{
return $this->_showHead();
}
}
?>
<?php
//使用
$p=new stringPage($print_content);
echo $p->show();
?>
所使用的js
//取得滑鼠的位置
function GetCursorPos(oTextArea)
{
var s="~!@#$%^";
clipboardData.setData('text',s);
oTextArea.focus();
document.execCommand('paste');
var ret=oTextArea.value.indexOf(s);
document.execCommand('undo');
if(ret==-1) GetCursorPos(oTextArea)
return ret;
}
function addtext(content)
{
var len=GetCursorPos(content);
var all = hxf.content.value;
//alert(len);
if(len>0 && len!=all.length)
{
//alert(hxf.content.value.length);
var old1= all.substring(0,len);
var old2= all.substring(len,all.length);
var format="<?=$FORMAT?>";
var tmp=old1+format+old2;
hxf.content.value=tmp;
var r=content.createTextRange();
r.moveStart('character',len);
r.collapse(true);
r.select();
//alert();
//setCaretAtEnd(content);
//hxf.content.focus();
}else if(len==0){
alert("不能在文章首設定分頁!");
}else if(len==all.length)
{
alert("不能在文章尾設定分頁!");
}
}
頁面使用
<input type="button" name="pagep" value="設定分頁" onclick="addtext(content)" >
傳遞文本地區的在表單裡的名稱!