PHP實(shí)現(xiàn)適用于文件內(nèi)容操作的分頁類
來源:易賢網(wǎng) 閱讀:896 次 日期:2016-08-17 14:39:59
溫馨提示:易賢網(wǎng)小編為您整理了“PHP實(shí)現(xiàn)適用于文件內(nèi)容操作的分頁類”,方便廣大網(wǎng)友查閱!

本文實(shí)例為大家分享了PHP實(shí)現(xiàn)文件內(nèi)容操作的分頁類,強(qiáng)調(diào)一下只針對(duì)文件的操作,供大家參考,具體內(nèi)容如下

<?php 

class StrPage {

 private $current;  //當(dāng)前頁 

 private $file;  //操作文件

 private $totalPage;  //總的頁數(shù)

 private $url;  //傳遞的參數(shù)

 private $pageLen;  //每頁顯示的長(zhǎng)度

 function __construct( $file,$len = 200 ){

 $this->file = file_get_contents($file);

 $this->pageLen = $len;

 $this->current = isset($_GET['page'])?$_GET['page']:1;

 $this->totalPage = $this->getTotalPage();

 $this->url = $this->getUrl();

 }

 //獲取到這個(gè)文件總的長(zhǎng)度

 private function getTotalPage(){

 return ceil(strlen($this->file)/$this->pageLen);

 }

 //獲取當(dāng)前傳遞的參數(shù),保留參數(shù),page參數(shù)動(dòng)態(tài)變化

 private function getUrl(){

 $url =parse_url($_SERVER['REQUEST_URI']);

 parse_str($url['query'],$queryArr);

 unset($queryArr['page']);

 $queryStr = http_build_query($queryArr);

 return $url['path'].'?'.$queryStr.'&page='; 

 }

 //首頁

 private function first(){

 if($this->current>1)

  return "<a href='".$this->url."1'>首頁</a>";

 }

 //上一頁

 private function pre(){

 if( $this->current > 1 )

  return "<a href='".$this->url.($this->current-1)."'>上一頁</a>";

 }

 //下一頁

 private function next(){

 if( $this->current < $this->totalPage)

  return "<a href='".$this->url.($this->current+1)."'>下一頁</a>";

 }

 //最后一頁

 private function end(){

 if( $this->current < $this->totalPage )

  return "<a href='".$this->url.$this->totalPage."'>末頁</a>";

 }

 public function pageList(){

 $pageListStr = '';

 for ($i=1;$i<=$this->totalPage;$i++){

  if($i==$this->current){

  $pageListStr.="<span style='font-size:20px;color:#f00'>".$i."</span> ";

  }else{

  $pageListStr.="<a href='".$this->url.$i."'>".$i." </a>";

  }

 }

 return $pageListStr;

 }

 public function pageStyle($style=1){

  switch ($style){

  case 1:

   return "共有".$this->totalPage."頁".$this->first().$this->pre().$this->pageList().$this->next().$this->end();

   break;

  case 2;

  return $this->pageList();

  break;

  }

 }

 public function getContents(){

 $prePageLen = strlen($this->subStrs($this->current-1));

 $currentPageLen = strlen($this->subStrs($this->current));

 return substr($this->file, $prePageLen,$currentPageLen-$prePageLen);

 }

 public function subStrs($page){ 

  $string = '';

  $len= $page*$this->pageLen;

  for( $i=0; $i<$len; $i++ ){

   if( ord(substr($this->file,$i,1))>0xa0 ){

   $string .= substr($this->file,$i,3);

   $i = $i+2;

  }else{

   $string .= substr($this->file, $i,1);

  }

  }

  return $string;

 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP實(shí)現(xiàn)適用于文件內(nèi)容操作的分頁類
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2026國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)