您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP工具类

2024/3/11 15:05:09发布21次查看
php工具类,开发系统必备的类:
表单验证类
验证码类
日志类
分页类
无限极分类类 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 $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']; } }?>
复制代码
height = $height; $this->width = $width; } private function gencode($num) { for($i=0;$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 { $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 { $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();?>
复制代码
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 ; ?>
复制代码
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%7d>尾页  ; } 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; $itotalpage; $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();?>
复制代码
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; ?>复制代码
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product