仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1493|回复: 18
打印 上一主题 下一主题

[学习教程] PHP编程:玲珑的php文档生成类

[复制链接]
金色的骷髅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:30:26 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
总的来说,在这一个月左右的时间中,学到的不少,但是也遇到不少的问题,比如批量图片的上传,一直到现在也不懂,如何实现动态的增加上传图片的数量。   在项目开辟中发明对php的文档短少办理,他人写了一个,功效不多

<?php
/**
* 类名: doc
* 描写: 文档生成类
* 其他: 可以对目次停止过滤,设置好源目次后,请用相对途径指定生成目次,形式可调,形式
* 1为惯例类型,即以 斜线**开首,以*斜线 停止
* 2为扩大类型,但凡 斜线*开首以*斜线 停止的局部都将成为文档的一局部
*/
class doc
{
var $docdirname;
var $docdir;

/**
* 函数称号: doc()
* 函数功效: 机关
* 输出参数: none
* 函数前往值: 前往值申明
* 其它申明: 2004-10-13
*/
function doc()
{
$this->docdirname = "doc/";
}

/**
* 函数称号: createDoc($root,$newdir,$mode="1",$filter=null)
* 函数功效: 创立文档
* 输出参数: $root -------------- 源目次
$newdir ----------- 方针目次
$mode ------------- 形式,1为通俗,2为扩大
$filter ------------ 过滤目次
* 函数前往值: 前往值申明
* 其它申明: 2004-10-13
*/
function createDoc($root,$newdir,$mode="1",$filter=null)
{
$getarr = $this->loopDir($root,$filter);
$i = 0;
$this->createFrame($newdir);
foreach($getarr as $key=>$val)
{
if($this->getPhpFiles($val))
{
$content = $this->getContent($val);
$content = $this->getDoc($content,$mode);
$filepath = $this->setFilepath($val,$root,$newdir);
$filedir = $this->getFileDir($filepath);
$this->mkdirs($filedir);
$this->setDoc($filepath,$content);
$data[$i]['url'] = "$filepath";
$data[$i]['name'] = "$val";
$i++;
}
}
if(!empty($data))
{
$this->createMenu($newdir,$data);
$this->redirect($this->docdir);
}
}

/**
* 函数称号: redirect($path)
* 函数功效: 转向
* 输出参数: $path ---------------- 转向途径
* 函数前往值: 前往值申明
* 其它申明: 2004-10-13
*/
function redirect($path)
{
echo "<a href=".$path." target='_blank'>生成文档胜利,点击此处检查</a>";
}

/**
* 函数称号: loopDir($root,$filter=null)
* 函数功效: 遍历目次
* 输出参数: $root ------------------- 源目次
$filter ----------------- 过滤
* 函数前往值: array
* 其它申明: 2004-10-13
*/
function loopDir($root,$filter=null)
{
static $getarr=array();
$d = dir($root);
while (false !== ($entry = $d->read()))
{
if ($entry == "." || $entry == "..")
{
continue;
}
if($this->filter($entry,$filter))
{
if(is_dir($root.$entry))
{
$this->loopDir($d->path.$entry."/");
}
else
{
$getarr[] = $d->path.$entry;
}
}
}
$d->close();
Return $getarr;
}

/**
* 函数称号: getPhpFiles($path)
* 函数功效: 提取php文档
* 输出参数: $path ---------------- 文档途径
* 函数前往值: bool
* 其它申明: 2004-10-13
*/
function getPhpFiles($path)
{
$type = preg_replace('/.*\.(.*[^\.].*)/i','\\1',$path);
$type = strtolower($type);
if($type=="php")
{
Return true;
}
else
{
Return false;
}
}

/**
* 函数称号: getContent($path)
* 函数功效: 读取文件内容
* 输出参数: $path ------------------- 文件途径
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function getContent($path)
{
$fp = file($path);
$content = implode('',$fp);
Return $content;
}

/**
* 函数称号: getDoc($content,$mode="1")
* 函数功效: 掏出php文件中的正文
* 输出参数: $content ------------ 文档内容
$mode --------------- 形式,1为通俗,2为扩大
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function getDoc($content,$mode="1")
{
switch($mode)
{
case '1':
$pattern = '/\/(\*)[\r\n].*\*\//isU';
break;
case '2':
$pattern = '/\/\*.*\*\//isU';
break;
}

preg_match_all($pattern,$content,$carr);
$getarr = array();
foreach($carr[0] as $key=>$val)
{
$getarr[] = trim($val);
}
$str = implode("<br><br>",$getarr);
$str = preg_replace('/[\r]/i','<br>',$str);
$style = $this->getStyle();
$str = $this->getTable($str);
$str = $style.$str;
Return $str;
}

/**
* 函数称号: etFilepath($filepath,$oldroot,$newroot)
* 函数功效: 设置生成文件的途径
* 输出参数: $filepath -------------- 源文件途径
$oldroot -------------- 源目次途径
$newroot -------------- 方针目次途径
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function setFilepath($filepath,$oldroot,$newroot)
{
$oldroot = str_replace('/',"\\/",$oldroot);
$pattern = "/".$oldroot."(.*)/iU";
$filepath = preg_replace($pattern,'\\1',$filepath);
$newpath = $newroot.$this->docdirname.$filepath;//echo "$newpath<br>";
$newpath = preg_replace('/(.*\.)(.*[^\.].*)/i','\\1htm',$newpath);
Return $newpath;
}

/**
* 函数称号: getFileDir($path)
* 函数功效: 获得文档目次
* 输出参数: $path ------------- 文档途径
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function getFileDir($path)
{
$getpath = preg_replace('/(.*)(\/.*[^\.].*)/i','\\1',$path);
Return $getpath;
}

/**
* 函数称号: setDoc
* 函数功效: 将正文写入指定目次并生成页面
* 输出参数: $filepath --------------- 目次途径
$content ---------------- 写入的内容
* 函数前往值: 前往值申明
* 其它申明: 申明
*/
function setDoc($filepath,$content)
{
$fp = fopen($filepath,"w+");
flock($fp,LOCK_EX);
fwrite($fp,$content);
flock($fp, LOCK_UN);
}

/**
* 函数称号: mkdirs($path)
* 函数功效: 创立目次
* 输出参数: $path ------------------- 途径
* 函数前往值: none
* 其它申明: 2004-10-13
*/
function mkdirs($path)
{
$adir = explode('/',$path);
$dirlist = '';
$rootdir = $adir[0];
array_shift ($adir);
foreach($adir as $key=>$val)
{
if($val!='.'&&$val!='..')
{
$dirlist .= "/".$val;
$dirpath = $rootdir.$dirlist;
if(!file_exists($dirpath)&&!is_file($dirpath))
{
mkdir($dirpath);
chmod($dirpath,0777);
}
}
}
}

/**
* 函数称号: filter($item,$arr=null)
* 函数功效: 过滤
* 输出参数: $item -------------- 内容
$arr --------------- 过滤项
* 函数前往值: bool
* 其它申明: 2004-10-13
*/
function filter($item,$arr=null)
{
$item = strtolower($item);
$filter = explode(',',$arr);
if($arr==null||!in_array($item,$filter))
{
Return true;
}
else
{
Return false;
}
}

/**
* 函数称号: createFrame($root)
* 函数功效: 生成框架页
* 输出参数: $root --------------- 首页的寄存目次
* 函数前往值: str
* 其它申明: 2004-10-13
*/
function createFrame($root)
{
$str = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无题目文档</title>
</head>

<frameset cols="150,*" frameborder="YES" border="10" framespacing="5" bordercolor="#003366">
<frame src="menu.htm" name="leftFrame" framespacing="5" frameborder="auto" border="5" bordercolor="#f5f5f5" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" >
<frame src="#" name="mainFrame">
</frameset>
<noframes><body>
</body></noframes>
</html>';
$this->docdir = $root."index.htm";
$this->setDoc($this->docdir,$str);
}

/**
* 函数称号: createMenu($root,$data)
* 函数功效: 生成菜单
* 输出参数: $root ------------------- 页面存入目次
$data ------------------- 内容
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function createMenu($root,$data)
{
$path = $root."menu.htm";
$str = $this->getStyle();
$str.= "<table>";
foreach($data as $key=>$val)
{
$str.= "<tr><td><a href='".$val['url']."' target='mainFrame'>".$val['name']."</a></td></tr>";
}
$str.= "</table>";
$this->setDoc($path,$str);
}

/**
* 函数称号: getStyle()
* 函数功效: 款式
* 输出参数: none
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function getStyle()
{
$str = '
<style>
table {
font-family: "Courier New","宋体";
border-collapse: collapse;
word-break:break-all;
}
td {
font-family: "Courier New","宋体";
font-size: 12px;
line-height: 22px;
}
</style>';
Return $str;
}

/**
* 函数称号: getTable($content)
* 函数功效: 把内容放入table中
* 输出参数: $content ------------ 内容
* 函数前往值: string
* 其它申明: 2004-10-13
*/
function getTable($content)
{
$str = "<table width=\"100%\" border=\"1\" bordercolor=\"#dbdbdb\" cellpadding=\"5\" cellspacing=\"0\">
<tr>
<td bgcolor=\"#f5f5f5\">".$content."</td>
</tr>
</table>";
Return $str;
}
}

// 利用
$d = new doc;
$filter = "adodb,smarty,cvs,templates,templates_c";
$d->createDoc("e:/www/kpub20/class/","e:/www/test/aaa/",1,$filter);
?>

  当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与,
金色的骷髅 该用户已被删除
沙发
 楼主| 发表于 2015-3-24 06:04:23 | 显示全部楼层
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-9 18:57

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表