日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php分页功能乱码了怎么办,51、PHP文件内容分页操作,避免乱码

發(fā)布時間:2025/3/15 php 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php分页功能乱码了怎么办,51、PHP文件内容分页操作,避免乱码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

新浪等一些大平臺新聞網(wǎng)站經(jīng)常使用內(nèi)容分頁技術,把一篇文章分成若干部分,每一部分作為其中一頁,以這種形式展現(xiàn)給用戶,不僅提升了用戶體驗,也提高了網(wǎng)站加載內(nèi)容的效率,但是這種做法最大的問題就是在截取字符串的時候會出現(xiàn)亂碼,下面是我做的一個測試,基本沒什么問題,沒有亂碼出現(xiàn)。

代碼如下:

header('content-type:text/html;charset="utf-8"');

class StrPage{

private $file;//文件內(nèi)容

private $current;//當前頁

private $pageLen;//每頁顯示的內(nèi)容長度

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

private $url;//url地址

public function __construct($file,$len){

$a = array(' ',' ','\t','\n','\r','\s');

$b = array('','','','','');

$this->file = str_replace($a,$b,file_get_contents($file));

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

$this->pageLen = $len;

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

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

}

private function getTotalPage(){

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

}

private function getUrl(){

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

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

unset($arry['page']);

$query = http_build_query($arry);

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

}

private function first(){

if($this->current>1){

return '首頁??';

}

}

private function next(){

if($this->currenttotalPage){

return '下一頁??';

}

}

private function pre(){

if($this->current>1){

return '上一頁??';

}

}

private function end(){

return '末頁';

}

private function subStrs($str){

if(ord(substr($this->file,$str,1))>127){

if(strlen(substr($this->file,0,$str))%3==1){

return $str+=2;

}else if(strlen(substr($this->file,0,$str))%3==2){

return $str+=1;

}else{

return $str+=0;

}

}else{

return $str+=0;

}

}

public function getContents(){

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

$currentPageLen = $this->subStrs($this->current*$this->pageLen);

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

echo $prePageLen;

echo $currentPageLen;

return $string;

}

public function pageStyle(){

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

}

}

$php = new StrPage('file.txt',500);

echo '

'.$php->getContents().'';

echo '

';

echo $php->pageStyle();

echo '

';

注意,你的文件編碼和php腳本的編碼要一致,我這邊用的是utf8編碼,在截取字符串避免亂碼的時候與gbk稍有不同,詳情請看上一章節(jié)。

總結

以上是生活随笔為你收集整理的php分页功能乱码了怎么办,51、PHP文件内容分页操作,避免乱码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。