php 文章内容分页或者生成静态化分页文件的简单示例

前端之家收集整理的这篇文章主要介绍了php 文章内容分页或者生成静态化分页文件的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP文章内容分页或者生成静态分页文件感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

<?PHP /**
* @package myFramework create html class
* @Description Cut string
* @Environment : Apache2.0.59+PHP5.2.5+MysqL5.0
* @arrange (512.笔记) jb51.cc
* $test = <<<EOF
* EOF;
*
* $html = new myFrame_HTML(100);
* $html->createHtml($html->cut($test,1),'11');
*
*/
class myFrame_HTML extends myFrame
{
	//定义每页显示数量
	var $count = 5000;
	var $pageAmount;
	var $charSet = 'UTF-8';
	var $folder = 'html/';
	function __construct($count ='')
	{
	if($count != '')
	{
	$this->count = $count;
	}
	}
	/**
	* cut string for pages
	*
	* @param string $contents
	* @param int $page this is start page
	* @return array
	*/
	function cut($contents,$page = 1)
	{
	$total = mb_strlen($contents);
	if($total < $this->count)
	{
	$this->pageAmount['contents'][] = $contents;
	$this->pageAmount['page'][] = $page;
	}else{
	$this->pageAmount['contents'][] = mb_strcut($contents,$this->count,$this->charSet);
	$this->pageAmount['page'][] = $page;
	$almost = mb_strcut($contents,$total,$this->charSet);
	$this->cut($almost,$page+1);
	}
	return $this->pageAmount;
	}
	function createDynamic($array,$page,$pageName = 'page',$queryString = '')
	{
	$pageFoot = $array['page'];
	$contents = $array['contents'];
	$html = '';
	$count = sizeof($contents);
	for ($i = 0; $i < $count; $i++)
	{
	$thisFile = $fileName;
	if($count == 1)
	{
	$html[$pageFoot[$i]]= $contents[$i];
	}else{
	$html[$pageFoot[$i]]= $contents[$i] . $this->createDynamicFoot($pageFoot,$pageFoot[$i],$pageName,$queryString);
	}
	}
	return $html[$page];
	}
	function createDynamicFoot($page,$current,$queryString)
	{
	foreach ($page as $p)
	{
	if($p == $current)
	{
	$foot .= ' [ '. $p .' ]';
	}else{
	$foot .= ' [ <a href="?'. $queryString . '&' . $pageName .'=' . $p .'">'.$p.'</a> ]';
	}
	}
	return '<div class="pageFoot">' . $foot . '</div>';
	}
	/**
	* create html
	*
	* @param array $array
	* @param string $fileName
	*/
	function createHtml($array,$fileName)
	{
	$pageFoot = $array['page'];
	$contents = $array['contents'];
	$html = '';
	$count = sizeof($contents);
	for ($i = 0; $i < $count; $i++)
	{
	$thisFile = $fileName;
	if($count == 1)
	{
	$html= $contents[$i];
	$thisFile = $fileName . '.html';
	}else{
	$html= $contents[$i] . $this->createFoot($pageFoot,$fileName);
	if($pageFoot[$i] == 1)
	{
	$thisFile = $thisFile . '.html';
	}else{
	$thisFile = $thisFile . '_' . $pageFoot[$i] . '.html';
	}
	}
	file_put_contents($this->folder . $thisFile,$html);
	}
	}
	/**
	* create page index
	*
	* @param int $page
	* @param int $current
	* @param string $fileName
	* @return string
	*/
	function createFoot($page,$fileName)
	{
	$foot = '';
	foreach ($page as $p)
	{
	if($p == $current)
	{
	$foot .= ' [ '. $p .' ]';
	}else{
	if($p == 1)
	{
	$foot .= ' [ <a href="'. $fileName . '.html">'.$p.'</a> ]';
	}else{
	$foot .= ' [ <a href="'. $fileName. '_' .$p . '.html">'.$p.'</a> ]';
	}
	}
	}
	return '<div class="pageFoot">' . $foot . '</div>';
	}
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章