PHP工具類

WBOY
發布: 2016-07-25 08:48:39
原創
3011 人瀏覽過
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. {
  7. if(!is_array($post))
  8. {
  9. $this->msg[] = '資料不是備份';
  10. }
  11. else
  12. {
  13. foreach ($post as $field=>$value)
  14. {
  15. $func = $post[$field]['valid'];
  16. $value = $ post[$field]['value'];
  17. $checkLength = 'checkLength';
  18. if($pos = stripos($func , $checkLength)!=​​false)
  19. {
  20. $condition = substr($func, strlen($checkLength));
  21. $func = $checkLength;
  22. $lengthArr =explode('-', $condition);
  23. self :: $func($value , $field , $lengthArr[0] , $lengthArr[1]);
  24. }
  25. else
  26. {
  27. if(!in_array($func , $this- >typeArr) ))
  28. {
  29. $this->msg = $func。 isNotExists';
  30. 規則;
  31. }
  32. self::$func($value , $field);
  33. }
  34. }
  35. }
  36. return $this->showRestult ( );
  37. }
  38. 牛皮函數showRestult()
  39. {
  40. if($this->msg && is_array($this->msg))
  41. {
  42. $this ->code = 1;
  43. $msg = implode(',', $this->msg);
  44. $ret = array('code'=>$this->code , 'msg'=>$ msg);
  45. return $ret;
  46. }
  47. return array('code'=>$this->code , 'msg'=>'成功' );
  48. }
  49. private function isNotEmpty($value,$field)
  50. {
  51. if(!$this->hasSet($value, $field)) return false;
  52. $value = trim($value);
  53. if(empty($value))
  54. {
  55. $this->msg[] = $field.' isEmpty';
  56. return false;
  57. }
  58. return true;
  59. }
  60. private function isInt($value,$field)
  61. {
  62. if(! this->isNotEmpty($value,$field)) return false;
  63. $value = trim($value);
  64. if(!is_int($value))
  65. {
  66. $this- > msg[] = $字段。 isNotInt';
  67. return false;
  68. }
  69. return true;
  70. }
  71. private function isStr($value,$field)
  72. {
  73. if(!$ this->isNotEmpty($value,$field)) return false;
  74. $value = trim($value);
  75. if(!is_string($value))
  76. {
  77. $this- > msg[] = $字段。 isNotStr';
  78. return false;
  79. }
  80. return true;
  81. }
  82. private function hasSet($value , $field)
  83. {
  84. {
  85. if(!issfield)
  86. {
  87. if(! ($value))
  88. {
  89. $this->msg[] = $field.' isNotSet';
  90. return false;
  91. }
  92. return true;
  93. }
  94. private function isEmail($value,$field)
  95. {
  96. {
  97. if! this->isNotEmpty($value,$field)) return false;
  98. $value = trim($value);
  99. $pattern = "/^[a-zA-Z0-9_-] @[a - zA-Z0-9_-] (.[a-zA-Z0-9_-] )$/";
  100. if(!preg_match($pattern, $value))
  101. {
  102. $this- > msg[] = $字段。 isNotEmail';
  103. return false;
  104. }
  105. return true;
  106. }
  107. private function isTel($value,$field)
  108. {
  109. if(! this->isNotEmpty($value,$field)) return false;
  110. $value = trim($value);
  111. $pattern = '/^[0-9]{7,11}$/' ;
  112. if (!preg_match($pattern, $value))
  113. {
  114. $this->msg[] = $field.' isNotTel';
  115. return false;
  116. }
  117. return true;
  118. }
  119. private function isOnlyNum($value,$field)
  120. {
  121. {
  122. if( this->isNotEmpty($value,$field)) return false;
  123. $value = trim($value);
  124. $pattern = "/^[0-9]{1,}$/";
  125. if(!preg_match($pattern, $value))
  126. {
  127. $this->msg[] = $field.' isNotOnlyNum';
  128. return false;
  129. }
  130. return true;
  131. }
  132. private function isOnlyChar($value,$field)
  133. {isNotEmpty($value,$field)) return false;
  134. $value = trim($value);
  135. $pattern = "/^[a-zA-Z]{1,}$/ " ;
  136. if(!preg_match($pattern, $value))
  137. {
  138. $this->msg[] = $field.' isNotOnlyChar';
  139. return false;
  140. }
  141. return true;
  142. }
  143. private function isNumAndChar($value,$field)
  144. {isNotEmpty($value,$field)) return false;
  145. $value = trim($value);
  146. $pattern = "/^[a-zA-z0-9]{1,} $ /";
  147. if(!preg_match($pattern , $value))
  148. {
  149. $this->msg[] = $field.' isNotNumAndChar';
  150. 回傳 false;
  151. } 回傳 true; }
  152. 導管函數 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. 回傳 false;
  161. }
  162. 回傳 true;
  163. }
  164. }
  165. if($_POST['提交'])
  166. {
  167. if($_POST['提交'])
  168. {
  169. {
  170. $form = new Lib_Form();
  171. $post['name'] = array('value'=>$_POST['name'] , 'valid'=>'checkLength6-12');
  172. $ post['pwd'] = array('value'=>$_POST['pwd'] , 'valid'=>'checkLength4-12');
  173. $post['sex'] = array('value' =>$_POST['sex'] , 'valid'=>'hasSet');
  174. $ret = $form->validata($post);
  175. if ($ret['code' ])
  176. {
  177. echo $ret['msg'];
  178. }
  179. }
?>
複製代碼
  1. class Lib_Image
  2. {
  3. private $height = 0;
  4. private $width = 0;
  5. {
  6. $this->height = $height;
  7. $this->width = $width;
  8. }
  9. 私有函數genCode( $num)
  10. {
  11. for($i=0;$i {
  12. switch(rand(0,2))
  13. {
  14. case 0:$code[$i]=chr(rand(48,57));break;//數字
  15. case 1:$code[$i]=chr(rand(65) ,90 ));break;//大寫字母
  16. case 2:$code[$i]=chr(rand(97,122));break;//小寫字母
  17. }
  18. }
  19. $ _SESSION[ "驗證碼"]=$驗證碼;
  20. 回傳 $code;
  21. }
  22. private function genOther($image)
  23. {
  24. for($i=0;$i {
  25. $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  26. imagesetpixel($image,rand(1,$this->寬度),rand(1 , $this->height),$dis_color);
  27. }
  28. }
  29. public function veryCode()
  30. {
  31. $image=imagecreate($this- >寬度,$this->高度);
  32. imagecolorallocate($image,255,255,255);
  33. //$this->genOther($image);
  34. $num = 4;
  35. $code = $this->genCode($num);
  36. for($i=0;$i {
  37. $ char_color =imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  38. imagechar($image,60,($this->width/$num)*$i, rand( 0,5),$code[$i],$char_color);
  39. }
  40. header("內容類型:image/png");
  41. imagepng($image); //圖片輸出到瀏覽器
  42. imagedestroy($image);//釋放資源
  43. }
  44. }
$image = new Lib_Image(25, 65);
$image = new Lib_Image(25, 65); $image ->veryCode();?>
複製程式碼
  1. class Lib_Log
  2. {
  3. private $logError = 0;
  4. {
  5. private $logError = 0;
  6. private $privwarn = 1; 原生$logDir = 'log/';
  7. 原生$logFile = 'log';
  8. 原生$fileExt = '.txt';
  9. 原生$fileHander = null;
  10. public function __construct()
  11. {
  12. if(!is_dir($this->logDir)){
  13. mkdir($this->logDir,0777);
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. $this->logFile .= date('Y-m-d').$this->fileExt;
  42. if(!$this->fileHander = @fopen($this->logDir) .$this-> logFile, 'a ')){
  43. die('日誌檔打不開!');
}
} 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)."rn"; if(!fwrite($this->fileHander, $string) ) { die('日誌檔無法寫入!'); } } public function __destruct() { if($this- >fileHander! =null){ fclose($this->fileHander); } } } $log = new Lib_Log(); $log->writeLog('錯誤調試!'); echo ""; ?>複製程式碼
  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($totalNums);
  12. $this->perNums = intval($perNums = intval($perNums = mper) ;
  13. $this->totalPage = intval(ceil($this->totalNums / $this->perNums));
  14. $this->currentPage = min(max(1 , $_REQUEST['p'] ) , $this->totalPage);
  15. $this->type = intval($type);
  16. }
  17. private function first()
  18. {
  19. if ($this ->currentPage==1) return false;
  20. return "首頁  ";
  21. }
  22. private function last()
  23. {
  24. if ($this->currentPage==$this->totalPage) return false;
  25. return "尾頁  ";
  26. }
  27. private function next()
  28. {
  29. $p = min($this->currentPage 1 , $this->totalPage);
  30. if ($ p==$this->totalPage) return false;
  31. return "下一頁  ";
  32. }
  33. private function prev()
  34. {
  35. $p = max(1 , $this->currentPage - 1);
  36. if($p==1) return false;
  37. return "上一頁  ";
  38. }
  39. private function total()
  40. {
  41. return "共{$ this->totalPage} 頁 | {$this->totalNums} 筆記錄 | 目前第{$this->currentPage} 頁";
  42. }
  43. private function page()
  44. {
  45. $show = "";
  46. for ($i=1; $itotalPage; $i ){
  47. if ($i==$this->currentPage)
  48. $show .= "{$i}  " ;
  49. else
  50. $show .= "{$i}  ";
  51. }
  52. return $show;
  53. }
  54. public function show()
  55. {
  56. if ($this->type==1) {
  57. return $this->total().' '.$this- >page();
  58. }else if($this->type==2){
  59. return $this->total().' '.$this->first().' '.$this ->prev().' '.$this->next().' '.$this->last();
  60. }elseif ($this->type==0){
  61. return $this ->total().' '.$this->first().' '.$this->prev().' '.$this->page().' '.$this->next(). ' '.$this->last();
  62. }
  63. }
  64. }
  65. $totalNums = 80;
  66. $perNums = 10;
  67. $page = new Lib_Page($totalNums, $perNums);
  68. echo $page->show();
?>
複製程式碼
  1. class Lib_Tree
  2. {
  3. private $items = array();
  4. private $icon = arravate $items = array();
  5. private $icon = array( '✔ ,
  6. '  ├',
  7. '   ├',
  8. '     ├',
  9. '     🎜> private $field = array( 'id','name');
  10. public $ret = '; ';
  11. public function __construct($items)
  12. {
  13. $this->items = $items;
  14. }
  15. public function setIcon($icon)
  16. {
  17. $this->icon = $icon;
  18. }
  19. public function getChildren($pid)
  20. {
  21. {
  22. foreach ($this->items as $item)
  23. {
  24. if($item['pid']==$pid)
  25. {
  26. $children[] = $item;
  27. }
  28. }
  29. 回傳$children && is_array($children) ? $children : false;
  30. }
  31. public function getParent($id)
  32. {
  33. return $A. ->items[$this->items[$id]['pid']];
  34. }
  35. public function show($pid)
  36. {
  37. $children = $this- >getChildren($pid);
  38. if(!$children ) return false;
  39. foreach ($children as $child)
  40. {
  41. $this->ret.='
  42. ';
  43. $this->ret.='
  44. ';
  45. $ this->ret.='刪除; 修改';
  46. $this->ret. ='
  47. ';
  48. $this->show($child['id']);
  49. }
  50. }
  51. }
  52. $items = array(
  53. array('id'=>1 , 'name'=>'湖北', 'pid'=>0, 'level'=>0),
  54. array('id'= >2 , 'name'=>'武漢', 'pid'=>1, 'level'=>1),
  55. array('id'=>3 , 'name'=>'孝感', 'pid '=>1, 'level'=>1),
  56. array('id'=>4 , 'name'=>'廣東', 'pid'=>0, 'level'=>0),
  57. array('id'=>5 , 'name'=>'廣州', 'pid'=>4, 'level '=>1),
  58. array('id'=>6 , 'name' =>'深圳', 'pid'=>4, 'level'=>1),
  59. array('id'=>7 , 'name'=>'東莞', 'pid'=>4, ' level'=>1),
  60. array('id'=>8 , 'name'=>'宜昌', 'pid'=>1, 'level'=>1),
  61. array('id '=>9 , 'name'=>'雲夢' , 'pid'=>3, 'level'=>2),
  62. array('id'=>10 , 'name'=>'南山區', 'pid'=>6, 'level'=>2),
  63. array('id'=>11 , 'name'=>'寶安全', 'pid'=>6, 'level'= >2),
  64. array('id'=>12 , 'name'=>'倒店', 'pid'=>9, 'level'=>3),
  65. array('id'= >13 , 'name'=>'羅範大隊', 'pid'=>12, 'level'=>4),
  66. array('id'=>14 , 'name'=>'下範存', 'pid'=>13, 'level'=>5),
  67. );
  68. $tree = new Lib_Tree($items);
  69. $ tree->show(0);
  70. echo $tree->ret;
  71. ?>
  72. 複製程式碼
  73. 類別名稱操作
    '.$this->icon[$child['level']].$child['name'].'
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板