PHP工具类

WBOY
Freigeben: 2016-07-25 08:48:39
Original
3012 Leute haben es durchsucht
PHP工具类,开发系统必备的类:
表单验证类
验证码类
日志类
分页类
无限极分类类
  1. class Lib_Form
  2. {
  3. private $typeArr=array('isNotEmpty' , 'isInt' , 'isStr' , 'isEmail' , 'isTel' , 'isOnlyNum' , 'hasSet', 'isOnlyChar' , 'isNumAndChar' , 'checkLength');
  4. private $msg = array();
  5. private $code = 0;
  6. public function validata($post)
  7. {
  8. if(!is_array($post))
  9. {
  10. $this->msg[] = 'data is not array';
  11. }
  12. else
  13. {
  14. foreach ($post as $field=>$value)
  15. {
  16. $func = $post[$field]['valid'];
  17. $value = $post[$field]['value'];
  18. $checkLength = 'checkLength';
  19. if($pos = stripos($func , $checkLength)!==false)
  20. {
  21. $condition = substr($func, strlen($checkLength));
  22. $func = $checkLength;
  23. $lengthArr = explode('-', $condition);
  24. self::$func($value , $field , $lengthArr[0] , $lengthArr[1]);
  25. }
  26. else
  27. {
  28. if(!in_array($func , $this->typeArr))
  29. {
  30. $this->msg = $func.' isNotExists';
  31. break;
  32. }
  33. self::$func($value , $field);
  34. }
  35. }
  36. }
  37. return $this->showRestult();
  38. }
  39. private function showRestult()
  40. {
  41. if($this->msg && is_array($this->msg))
  42. {
  43. $this->code = 1;
  44. $msg = implode(',', $this->msg);
  45. $ret = array('code'=>$this->code , 'msg'=>$msg);
  46. return $ret;
  47. }
  48. return array('code'=>$this->code , 'msg'=>'success');
  49. }
  50. private function isNotEmpty($value,$field)
  51. {
  52. if(!$this->hasSet($value, $field)) return false;
  53. $value = trim($value);
  54. if(empty($value))
  55. {
  56. $this->msg[] = $field.' isEmpty';
  57. return false;
  58. }
  59. return true;
  60. }
  61. private function isInt($value,$field)
  62. {
  63. if(!$this->isNotEmpty($value,$field)) return false;
  64. $value = trim($value);
  65. if(!is_int($value))
  66. {
  67. $this->msg[] = $field.' isNotInt';
  68. return false;
  69. }
  70. return true;
  71. }
  72. private function isStr($value,$field)
  73. {
  74. if(!$this->isNotEmpty($value,$field)) return false;
  75. $value = trim($value);
  76. if(!is_string($value))
  77. {
  78. $this->msg[] = $field.' isNotStr';
  79. return false;
  80. }
  81. return true;
  82. }
  83. private function hasSet($value , $field)
  84. {
  85. if(!isset($value))
  86. {
  87. $this->msg[] = $field.' isNotSet';
  88. return false;
  89. }
  90. return true;
  91. }
  92. private function isEmail($value,$field)
  93. {
  94. if(!$this->isNotEmpty($value,$field)) return false;
  95. $value = trim($value);
  96. $pattern = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)$/";
  97. if(!preg_match($pattern, $value))
  98. {
  99. $this->msg[] = $field.' isNotEmail';
  100. return false;
  101. }
  102. return true;
  103. }
  104. private function isTel($value,$field)
  105. {
  106. if(!$this->isNotEmpty($value,$field)) return false;
  107. $value = trim($value);
  108. $pattern = '/^[0-9]{7,11}$/';
  109. if (!preg_match($pattern, $value))
  110. {
  111. $this->msg[] = $field.' isNotTel';
  112. return false;
  113. }
  114. return true;
  115. }
  116. private function isOnlyNum($value,$field)
  117. {
  118. if(!$this->isNotEmpty($value,$field)) return false;
  119. $value = trim($value);
  120. $pattern = "/^[0-9]{1,}$/";
  121. if(!preg_match($pattern, $value))
  122. {
  123. $this->msg[] = $field.' isNotOnlyNum';
  124. return false;
  125. }
  126. return true;
  127. }
  128. private function isOnlyChar($value,$field)
  129. {
  130. if(!$this->isNotEmpty($value,$field)) return false;
  131. $value = trim($value);
  132. $pattern = "/^[a-zA-Z]{1,}$/";
  133. if(!preg_match($pattern, $value))
  134. {
  135. $this->msg[] = $field.' isNotOnlyChar';
  136. return false;
  137. }
  138. return true;
  139. }
  140. private function isNumAndChar($value,$field)
  141. {
  142. if(!$this->isNotEmpty($value,$field)) return false;
  143. $value = trim($value);
  144. $pattern = "/^[a-zA-z0-9]{1,}$/";
  145. if(!preg_match($pattern , $value))
  146. {
  147. $this->msg[] = $field.' isNotNumAndChar';
  148. return false;
  149. }
  150. return true;
  151. }
  152. private function checkLength($value , $field , $minLength , $maxLength)
  153. {
  154. if(!$this->isNotEmpty($value,$field)) return false;
  155. $value = trim($value);
  156. $length = (strlen($value) + mb_strlen($value,'UTF8')) / 2;
  157. if($length $maxLength)
  158. {
  159. $this->msg[] = $field.' isNotInLength';
  160. return false;
  161. }
  162. return true;
  163. }
  164. }
  165. if($_POST['submit'])
  166. {
  167. $form = new Lib_Form();
  168. $post['name'] = array('value'=>$_POST['name'] , 'valid'=>'checkLength6-12');
  169. $post['pwd'] = array('value'=>$_POST['pwd'] , 'valid'=>'checkLength4-12');
  170. $post['sex'] = array('value'=>$_POST['sex'] , 'valid'=>'hasSet');
  171. $ret = $form->validata($post);
  172. if($ret['code'])
  173. {
  174. echo $ret['msg'];
  175. }
  176. }
  177. ?>
复制代码
  1. class Lib_Image
  2. {
  3. private $height = 0;
  4. private $width = 0;
  5. public function __construct($height , $width)
  6. {
  7. $this->height = $height;
  8. $this->width = $width;
  9. }
  10. private function genCode($num)
  11. {
  12. for($i=0;$i {
  13. switch(rand(0,2))
  14. {
  15. case 0:$code[$i]=chr(rand(48,57));break;//数字
  16. case 1:$code[$i]=chr(rand(65,90));break;//大写字母
  17. case 2:$code[$i]=chr(rand(97,122));break;//小写字母
  18. }
  19. }
  20. $_SESSION["VerifyCode"]=$code;
  21. return $code;
  22. }
  23. private function genOther($image)
  24. {
  25. for($i=0;$i {
  26. $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  27. imagesetpixel($image,rand(1,$this->width),rand(1,$this->height),$dis_color);
  28. }
  29. }
  30. public function veryCode()
  31. {
  32. $image=imagecreate($this->width,$this->height);
  33. imagecolorallocate($image,255,255,255);
  34. //$this->genOther($image);
  35. $num = 4;
  36. $code = $this->genCode($num);
  37. for($i=0;$i {
  38. $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  39. imagechar($image,60,($this->width/$num)*$i,rand(0,5),$code[$i],$char_color);
  40. }
  41. header("Content-type:image/png");
  42. imagepng($image);//输出图像到浏览器
  43. imagedestroy($image);//释放资源
  44. }
  45. }
  46. $image = new Lib_Image(25, 65);
  47. $image->veryCode();
  48. ?>
复制代码
  1. class Lib_Log
  2. {
  3. private $logError = 0;
  4. private $logWarn = 1;
  5. private $logDebug = 2;
  6. private $logDir = 'log/';
  7. private $logFile = 'log';
  8. private $fileExt = '.txt';
  9. private $fileHander = null;
  10. public function __construct()
  11. {
  12. if(!is_dir($this->logDir)){
  13. mkdir($this->logDir,0777);
  14. }
  15. $this->logFile .= date('Y-m-d').$this->fileExt;
  16. if(!$this->fileHander = @fopen($this->logDir.$this->logFile, 'a+')){
  17. die('the log file can not be open!');
  18. }
  19. }
  20. public function writeLog($message)
  21. {
  22. $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
  23. $debug = debug_backtrace(true);
  24. $string = date('Y-m-d H:i:s')."\t";
  25. $string .= $ip."\t";
  26. $string .=$debug[0]['file']."\t";
  27. $string .= "\tline" . $debug[0]['line']."\t";
  28. $string .= json_encode($message)."\r\n";
  29. if(!fwrite($this->fileHander, $string)){
  30. die('the log file can not be written!');
  31. }
  32. }
  33. public function __destruct()
  34. {
  35. if($this->fileHander!=null){
  36. fclose($this->fileHander);
  37. }
  38. }
  39. }
  40. $log = new Lib_Log();
  41. $log->writeLog('the error debug!');
  42. echo "";
  43. ?>
复制代码
  1. class Lib_Page
  2. {
  3. public $currentPage=0; //当前页数
  4. private $totalPage=0; //总页数
  5. private $totalNums=0; //总记录数
  6. private $perNums=0; //每页显示的记录数
  7. private $type = 0; //显示类型
  8. public function __construct($totalNums , $perNums,$type=0)
  9. {
  10. $this->totalNums = intval($totalNums);
  11. $this->perNums = intval($perNums);
  12. $this->totalPage = intval(ceil($this->totalNums / $this->perNums));
  13. $this->currentPage = min(max(1 , $_REQUEST['p']) , $this->totalPage);
  14. $this->type = intval($type);
  15. }
  16. private function first()
  17. {
  18. if ($this->currentPage==1) return false;
  19. return "首页  ";
  20. }
  21. private function last()
  22. {
  23. if ($this->currentPage==$this->totalPage) return false;
  24. return "尾页  ";
  25. }
  26. private function next()
  27. {
  28. $p = min($this->currentPage+1 , $this->totalPage);
  29. if ($p==$this->totalPage) return false;
  30. return "下一页  ";
  31. }
  32. private function prev()
  33. {
  34. $p = max(1 , $this->currentPage - 1);
  35. if($p==1) return false;
  36. return "上一页  ";
  37. }
  38. private function total()
  39. {
  40. return "共 {$this->totalPage} 页 | {$this->totalNums} 条记录 | 当前第 {$this->currentPage} 页";
  41. }
  42. private function page()
  43. {
  44. $show = "";
  45. for ($i=1; $itotalPage; $i++){
  46. if ($i==$this->currentPage)
  47. $show .= "{$i}  ";
  48. else
  49. $show .= "{$i}  ";
  50. }
  51. return $show;
  52. }
  53. public function show()
  54. {
  55. if ($this->type==1) {
  56. return $this->total().' '.$this->page();
  57. }else if($this->type==2){
  58. return $this->total().' '.$this->first().' '.$this->prev().' '.$this->next().' '.$this->last();
  59. }elseif ($this->type==0){
  60. return $this->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next().' '.$this->last();
  61. }
  62. }
  63. }
  64. $totalNums = 80;
  65. $perNums = 10;
  66. $page = new Lib_Page($totalNums, $perNums);
  67. echo $page->show();
  68. ?>
复制代码
  1. class Lib_Tree
  2. {
  3. private $items = array();
  4. private $icon = array(
  5. '├',
  6. '  ├',
  7. '    ├',
  8. '      ├',
  9. '        ├',
  10. '          └',
  11. );
  12. private $field = array('id','name');
  13. public $ret = '';
  14. public function __construct($items)
  15. {
  16. $this->items = $items;
  17. }
  18. public function setIcon($icon)
  19. {
  20. $this->icon = $icon;
  21. }
  22. public function getChildren($pid)
  23. {
  24. foreach ($this->items as $item)
  25. {
  26. if($item['pid']==$pid)
  27. {
  28. $children[] = $item;
  29. }
  30. }
  31. return $children && is_array($children) ? $children : false;
  32. }
  33. public function getParent($id)
  34. {
  35. return $this->items[$this->items[$id]['pid']];
  36. }
  37. public function show($pid)
  38. {
  39. $children = $this->getChildren($pid);
  40. if(!$children) return false;
  41. foreach ($children as $child)
  42. {
  43. $this->ret.='
  44. ';
  45. $this->ret.='
  46. ';
  47. $this->ret.='
  48. ';
  49. $this->ret.='
  50. ';
  51. $this->show($child['id']);
  52. }
  53. }
  54. }
  55. $items = array(
  56. array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0),
  57. array('id'=>2 , 'name'=>'武汉', 'pid'=>1, 'level'=>1),
  58. array('id'=>3 , 'name'=>'孝感', 'pid'=>1, 'level'=>1),
  59. array('id'=>4 , 'name'=>'广东', 'pid'=>0, 'level'=>0),
  60. array('id'=>5 , 'name'=>'广州', 'pid'=>4, 'level'=>1),
  61. array('id'=>6 , 'name'=>'深圳', 'pid'=>4, 'level'=>1),
  62. array('id'=>7 , 'name'=>'东莞', 'pid'=>4, 'level'=>1),
  63. array('id'=>8 , 'name'=>'宜昌', 'pid'=>1, 'level'=>1),
  64. array('id'=>9 , 'name'=>'云梦', 'pid'=>3, 'level'=>2),
  65. array('id'=>10 , 'name'=>'南山区', 'pid'=>6, 'level'=>2),
  66. array('id'=>11 , 'name'=>'宝安全', 'pid'=>6, 'level'=>2),
  67. array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3),
  68. array('id'=>13 , 'name'=>'罗范大队', 'pid'=>12, 'level'=>4),
  69. array('id'=>14 , 'name'=>'下范存', 'pid'=>13, 'level'=>5),
  70. );
  71. $tree = new Lib_Tree($items);
  72. $tree->show(0);
  73. echo $tree->ret;
  74. ?>复制代码
  75. 类名 操作
    '.$this->icon[$child['level']].$child['name'].' 删除 添加 修改


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage