PHP工具类,开发系统必备的类: 表单验证类 验证码类 日志类 分页类 无限极分类类
class Lib_Form { private $typeArr=array('isNotEmpty' , 'isInt' , 'isStr' , 'isEmail' , 'isTel' , 'isOnlyNum' , 'hasSet', 'isOnlyChar' , 'isNumAndChar' , 'checkLength'); private $msg = array(); private $code = 0; public function validata($post) { if(!is_array($post)) { $this->msg[] = 'data is not array'; } else { foreach ($post as $field=>$value) { $func = $post[$field]['valid']; $value = $post[$field]['value']; $checkLength = 'checkLength'; if($pos = stripos($func , $checkLength)!==false) { $condition = substr($func, strlen($checkLength)); $func = $checkLength; $lengthArr = explode('-', $condition); self::$func($value , $field , $lengthArr[0] , $lengthArr[1]); } else { if(!in_array($func , $this->typeArr)) { $this->msg = $func.' isNotExists'; break; } self::$func($value , $field); } } } return $this->showRestult(); } private function showRestult() { if($this->msg && is_array($this->msg)) { $this->code = 1; $msg = implode(',', $this->msg); $ret = array('code'=>$this->code , 'msg'=>$msg); return $ret; } return array('code'=>$this->code , 'msg'=>'success'); } private function isNotEmpty($value,$field) { if(!$this->hasSet($value, $field)) return false; $value = trim($value); if(empty($value)) { $this->msg[] = $field.' isEmpty'; return false; } return true; } private function isInt($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); if(!is_int($value)) { $this->msg[] = $field.' isNotInt'; return false; } return true; } private function isStr($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); if(!is_string($value)) { $this->msg[] = $field.' isNotStr'; return false; } return true; } private function hasSet($value , $field) { if(!isset($value)) { $this->msg[] = $field.' isNotSet'; return false; } return true; } private function isEmail($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $pattern = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)$/"; if(!preg_match($pattern, $value)) { $this->msg[] = $field.' isNotEmail'; return false; } return true; } private function isTel($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $pattern = '/^[0-9]{7,11}$/'; if (!preg_match($pattern, $value)) { $this->msg[] = $field.' isNotTel'; return false; } return true; } private function isOnlyNum($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $pattern = "/^[0-9]{1,}$/"; if(!preg_match($pattern, $value)) { $this->msg[] = $field.' isNotOnlyNum'; return false; } return true; } private function isOnlyChar($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $pattern = "/^[a-zA-Z]{1,}$/"; if(!preg_match($pattern, $value)) { $this->msg[] = $field.' isNotOnlyChar'; return false; } return true; } private function isNumAndChar($value,$field) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $pattern = "/^[a-zA-z0-9]{1,}$/"; if(!preg_match($pattern , $value)) { $this->msg[] = $field.' isNotNumAndChar'; return false; } return true; } private function checkLength($value , $field , $minLength , $maxLength) { if(!$this->isNotEmpty($value,$field)) return false; $value = trim($value); $length = (strlen($value) + mb_strlen($value,'UTF8')) / 2; if($length < $minLength || $length > $maxLength) { $this->msg[] = $field.' isNotInLength'; return false; } return true; } } if($_POST['submit']) { $form = new Lib_Form(); $post['name'] = array('value'=>$_POST['name'] , 'valid'=>'checkLength6-12'); $post['pwd'] = array('value'=>$_POST['pwd'] , 'valid'=>'checkLength4-12'); $post['sex'] = array('value'=>$_POST['sex'] , 'valid'=>'hasSet'); $ret = $form->validata($post); if($ret['code']) { echo $ret['msg']; } } ?>
class Lib_Image { private $height = 0; private $width = 0; public function __construct($height , $width) { $this->height = $height; $this->width = $width; } private function genCode($num) { for($i=0;$i<$num;$i++)//生成验证码 { switch(rand(0,2)) { case 0:$code[$i]=chr(rand(48,57));break;//数字 case 1:$code[$i]=chr(rand(65,90));break;//大写字母 case 2:$code[$i]=chr(rand(97,122));break;//小写字母 } } $_SESSION["VerifyCode"]=$code; return $code; } private function genOther($image) { for($i=0;$i<80;$i++)//生成干扰像素 { $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagesetpixel($image,rand(1,$this->width),rand(1,$this->height),$dis_color); } } public function veryCode() { $image=imagecreate($this->width,$this->height); imagecolorallocate($image,255,255,255); //$this->genOther($image); $num = 4; $code = $this->genCode($num); for($i=0;$i<$num;$i++)//打印字符到图像 { $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagechar($image,60,($this->width/$num)*$i,rand(0,5),$code[$i],$char_color); } header("Content-type:image/png"); imagepng($image);//输出图像到浏览器 imagedestroy($image);//释放资源 } } $image = new Lib_Image(25, 65); $image->veryCode(); ?>
class Lib_Log { private $logError = 0; private $logWarn = 1; private $logDebug = 2; private $logDir = 'log/'; private $logFile = 'log'; private $fileExt = '.txt'; private $fileHander = null; public function __construct() { if(!is_dir($this->logDir)){ mkdir($this->logDir,0777); } $this->logFile .= date('Y-m-d').$this->fileExt; if(!$this->fileHander = @fopen($this->logDir.$this->logFile, 'a+')){ die('the log file can not be open!'); } } public function writeLog($message) { $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; $debug = debug_backtrace(true); $string = date('Y-m-d H:i:s')."\t"; $string .= $ip."\t"; $string .=$debug[0]['file']."\t"; $string .= "\tline" . $debug[0]['line']."\t"; $string .= json_encode($message)."\r\n"; if(!fwrite($this->fileHander, $string)){ die('the log file can not be written!'); } } public function __destruct() { if($this->fileHander!=null){ fclose($this->fileHander); } } } $log = new Lib_Log(); $log->writeLog('the error debug!'); echo ""; ?>
class Lib_Page { public $currentPage=0; //当前页数 private $totalPage=0; //总页数 private $totalNums=0; //总记录数 private $perNums=0; //每页显示的记录数 private $type = 0; //显示类型 public function __construct($totalNums , $perNums,$type=0) { $this->totalNums = intval($totalNums); $this->perNums = intval($perNums); $this->totalPage = intval(ceil($this->totalNums / $this->perNums)); $this->currentPage = min(max(1 , $_REQUEST['p']) , $this->totalPage); $this->type = intval($type); } private function first() { if ($this->currentPage==1) return false; return "首页 "; } private function last() { if ($this->currentPage==$this->totalPage) return false; return "totalPage}'>尾页 "; } private function next() { $p = min($this->currentPage+1 , $this->totalPage); if ($p==$this->totalPage) return false; return "下一页 "; } private function prev() { $p = max(1 , $this->currentPage - 1); if($p==1) return false; return "上一页 "; } private function total() { return "共 {$this->totalPage} 页 | {$this->totalNums} 条记录 | 当前第 {$this->currentPage} 页 "; } private function page() { $show = ""; for ($i=1; $i<=$this->totalPage; $i++){ if ($i==$this->currentPage) $show .= "{$i} "; else $show .= "{$i} "; } return $show; } public function show() { if ($this->type==1) { return $this->total().' '.$this->page(); }else if($this->type==2){ return $this->total().' '.$this->first().' '.$this->prev().' '.$this->next().' '.$this->last(); }elseif ($this->type==0){ return $this->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next().' '.$this->last(); } } } $totalNums = 80; $perNums = 10; $page = new Lib_Page($totalNums, $perNums); echo $page->show(); ?>
class Lib_Tree { private $items = array(); private $icon = array( '├', ' ├', ' ├', ' ├', ' ├', ' └', ); private $field = array('id','name'); public $ret = '类名 操作 '; public function __construct($items) { $this->items = $items; } public function setIcon($icon) { $this->icon = $icon; } public function getChildren($pid) { foreach ($this->items as $item) { if($item['pid']==$pid) { $children[] = $item; } } return $children && is_array($children) ? $children : false; } public function getParent($id) { return $this->items[$this->items[$id]['pid']]; } public function show($pid) { $children = $this->getChildren($pid); if(!$children) return false; foreach ($children as $child) { $this->ret.=''; $this->ret.=''.$this->icon[$child['level']].$child['name'].' '; $this->ret.='删除 添加 修改 '; $this->ret.=' '; $this->show($child['id']); } } } $items = array( array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0), array('id'=>2 , 'name'=>'武汉', 'pid'=>1, 'level'=>1), array('id'=>3 , 'name'=>'孝感', 'pid'=>1, 'level'=>1), array('id'=>4 , 'name'=>'广东', 'pid'=>0, 'level'=>0), array('id'=>5 , 'name'=>'广州', 'pid'=>4, 'level'=>1), array('id'=>6 , 'name'=>'深圳', 'pid'=>4, 'level'=>1), array('id'=>7 , 'name'=>'东莞', 'pid'=>4, 'level'=>1), array('id'=>8 , 'name'=>'宜昌', 'pid'=>1, 'level'=>1), array('id'=>9 , 'name'=>'云梦', 'pid'=>3, 'level'=>2), array('id'=>10 , 'name'=>'南山区', 'pid'=>6, 'level'=>2), array('id'=>11 , 'name'=>'宝安全', 'pid'=>6, 'level'=>2), array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3), array('id'=>13 , 'name'=>'罗范大队', 'pid'=>12, 'level'=>4), array('id'=>14 , 'name'=>'下范存', 'pid'=>13, 'level'=>5), ); $tree = new Lib_Tree($items); $tree->show(0); echo $tree->ret; ?>
内容总结
以上是互联网集市为您收集整理的PHP工具类 全部内容,希望文章能够帮你解决PHP工具类 所遇到的程序开发问题。
如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
来源:【匿名】
更多 ►
【PHP工具类】教程文章相关的互联网学习教程文章
====================ImageUploadTool========================<?phpclass ImageUploadTool
{private$file; //文件信息private$fileList; //文件列表private$inputName; //标签名称private$uploadPath; //上传路径private$fileMaxSize; //最大尺寸private$uploadFiles; //上传文件//允许上传的文件类型private$allowExt = array(‘bmp‘, ‘jpg‘, ‘jpeg‘, ‘png‘, ‘gif‘);/*** ImageUploadTool con...
php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存
一、文件缓存
二、数据查询结果缓存,使用内存来实现高速缓存
本例主要使用文件缓存。
主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。
工具类代码:
// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录* $lifetime : 缓存文件有效期,单位为秒* $cacheid : 缓存文件路径,包含...
本文给大家分享的是php实现网页缓存的工具类的代码及使用方法,非常的实用,有需要的小伙伴可以参考下。php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存一、文件缓存二、数据查询结果缓存,使用内存来实现高速缓存本例主要使用文件缓存。主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。工具类代码:// 文件缓存类
class Cache {/**...
php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存
一、文件缓存
二、数据查询结果缓存,使用内存来实现高速缓存
本例主要使用文件缓存。
主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。
工具类代码:// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录* $lifetime : 缓存文件有效期,单位为秒* $cacheid : 缓存文件路径,包含...
php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存
一、文件缓存
二、数据查询结果缓存,使用内存来实现高速缓存
本例主要使用文件缓存。
主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。
工具类代码:
// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录* $lifetime : 缓存文件有效期,单位为秒* $cacheid : 缓存文件路径,包含...
php 缓存工具类 实现网页缓存,php工具类php 缓存工具类 实现网页缓存
php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存
一、文件缓存
二、数据查询结果缓存,使用内存来实现高速缓存
本例主要使用文件缓存。
主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。工具类代码:// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录*...
php实现网页缓存的工具类分享,php网页缓存工具类php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存
一、文件缓存
二、数据查询结果缓存,使用内存来实现高速缓存
本例主要使用文件缓存。
主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。
工具类代码:
// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录* $lifetime : 缓存文...
php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存一、文件缓存二、数据查询结果缓存,使用内存来实现高速缓存本例主要使用文件缓存。主要原理使用缓存函数来存储网页显示结果,如果在规定时间里再次调用则可以加载缓存文件。工具类代码:// 文件缓存类
class Cache {/*** $dir : 缓存文件存放目录* $lifetime : 缓存文件有效期,单位为秒* $cach...
好久没写php了,突然做个实验,发现竟然没有日志,哎 FileName = substr($filename,0, $dot_offset); else $this->FileName = $filename; $this->FilePath = $dir; $this->m_MaxLogFileNum = intval($maxlogfilenum); $this->m_RotaParam = intval($rotaparam); $this->m_RotaType = intval($rotatype); $this->m_Priority = intval($priority); $this->m_LogCount...
这篇文章主要介绍了PHP解析xml格式数据工具类,涉及php针对xml格式数据节点添加、获取、解析等相关操作技巧,需要的朋友可以参考下本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:class ome_xml {/*** xml资源** @var resource* @see xml_parser_create()*/public $parser;/*** 资源编码** @var string*/public $srcenc;/*** target encoding** @var string*/public $dstenc;/*** the origin...
这篇文章主要介绍了PHP实现可添加水印与生成缩略图的图片处理工具类,涉及php针对图片的显示、保存、压缩、水印等相关操作技巧,需要的朋友可以参考下本文实例讲述了PHP实现可添加水印与生成缩略图的图片处理工具类。分享给大家供大家参考,具体如下:ImageTool.class.php<?php
class ImageTool
{private $imagePath;//图片路径private $outputDir;//输出文件夹private $memoryImg;//内存图像public function __construct($imagePath...
这篇文章主要介绍了PHP实现可精确验证身份证号码的工具类,涉及php针对字符串的截取、计算、判断等相关操作技巧,并附带说明了身份证号码的原理,需要的朋友可以参考下本文实例讲述了PHP实现可精确验证身份证号码的工具类。分享给大家供大家参考,具体如下:<?php
class check_IdCard {// $num为身份证号码,$checkSex:1为男,2为女,不输入为不验证public function checkIdentity($num, $checkSex = ) { // 不是15位或不是18位都是无...
这篇文章主要介绍了PHP实现基于面向对象的mysqli扩展库增删改查操作工具类,结合实例形式分析了mysqli增删改查操作类的封装与使用技巧,需要的朋友可以参考下mysqli扩展库是MySQL扩展库的改进版本,在mysql扩展库的基础上提高了稳定性和效率,mysqli扩展库有两套东西,一套就是面向过程的mysqli另一套是面向对象的mysqli。操作方式大体和mysql扩展库大体一致,这次还是先抽取出来一个操作mysql的工具类,和调用的类。1. mysqli扩展库...
这次给大家带来PHP中文工具类ChineseUtil怎样转换汉字与拼音,PHP中文工具类ChineseUtil转换汉字与拼音的注意事项有哪些,下面就是实战案例,一起来看一下。PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转。PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion目前本类库拥有的三个功能,都是在实际开发过程中整理出来的。这次使用的数据不同于以前我开源过汉字转拼...
这篇文章主要介绍了PHP实现的获取文件mimes类型工具类,结合实例形式分析了php针对文件mimes类型的相关判断操作技巧,需要的朋友可以参考下本文实例讲述了PHP实现的获取文件mimes类型工具类。分享给大家供大家参考,具体如下:<?php
/** Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.** Licensed under the Apache License, Version 2.0 (the "License").* You may not use this file except in co...