長文章分頁類(自家用)

來源:互聯網
上載者:User

這個類並不難,思路來自分頁,思想是在內容裡加入不在頁面顯示的標籤, 作為分頁的標誌!

類中有個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="/^(&nbsp|<p>|&nbsp;){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>&nbsp;&nbsp;";
  }
  if($this->sumpage>1&&$this->curPage<$this->sumpage)
  {
   $head.= "<a href=".$url.$downPage.">下一頁</a>&nbsp;&nbsp;";
  }
  $head.="第{$this->curPage}頁&nbsp;&nbsp;共{$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)" >

傳遞文本地區的在表單裡的名稱!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.