PHP 文本文章分頁代碼 按標記或長度(不涉及資料庫)_php執行個體

來源:互聯網
上載者:User
執行個體代碼:
複製代碼 代碼如下:
/**
* **********************************************************
* Read Me
* 文章分頁
*
* 分頁方式,可以按字數分頁,按換行分頁,按特殊標記分頁等
* 其實實現思路是一樣的,只是將其按一定規律放入一個數組
* 然後根據 url 傳入的參數取得某個片段即可
* 大家完全可以寫一個功能強大的函數儲存起來以備不時之需
*
* 題外話:很多編輯器都有插入分頁按鈕,利用插入的代碼可顯示分頁
*
* filename: page.php
* charset: UTF-8
* create date: 2012-5-16
* **********************************************************
* @author itbdw
* @copyright (C) 2011-2012 itbdw
* @link http://weibo.com/itbudaoweng
*/
header('Content-Type:text/html; charset=utf-8');
?>
$title = 'Pagination Test';
//需要分頁的資料
$data = <<
Hey, guys. I am here to test if it is working.
This pagination is very simple, isn't it?
And I tried to use different method to page it.
Can you see it?
DATA;
//當前文章頁
$page = 0;
//初始文章長度
$length = 0;
//分頁長度
$perpage = 160;
//顯示在頁面的代碼
$link = '';
//分割後的數組
$strArr = array();
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$length = strlen($data);
//按字數分割
// $str = str_split($data, $perpage);
//按字元分割
$delimiter = "\n";
// $delimiter = '<--pagination-->';
$strArr = explode($delimiter, $data);
$strNum = count($strArr);
$content = $strArr[$page];
if ($strNum > 1) {
if ($page != 0) {
$link .= '首頁';
} else {
$link .= '首頁';
}
for ($n = 0; $n < $strNum; $n++) {
if ($n == $page) {
$link .= '' . ($n + 1) . '';
} else {
$link .= "" . ($n + 1) . "";
}
}
$link .= '';
if ($page != ($strNum - 1)) {
$link .= "尾頁";
} else {
$link .= '尾頁';
}
}
?>





測試文章分頁








  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.