首页 > 开发 > PHP > 正文

PHP常用处理静态操作类

2024-05-04 23:33:55
字体:大 中 小
来源:转载
供稿:网友

本文给大家分享的是我们在php开发的时候经常需要用到的一些静态操作类,都是个人整理的,推荐给大家,有需要的小伙伴可以参考下。

详细一个PHP开发时常用处理的操作类 - 希望大家多多补充 - 完善这个操作类

 

 
  1. <?php 
  2. /** 
  3. * 常用静态类,这里主要整理了一些PHP常常会用到的方法。 
  4. * 
  5. * @author ZCStrong - youkuiyuan 
  6. */ 
  7. class C { 
  8. /* 
  9. * 私有处理随机数的内置参数 
  10. * array 随机数数组/param 随机数长度 
  11. * 返回一个随机数 
  12. */ 
  13. static private function Random($array , $param) { 
  14. $randArray = $array; 
  15. $randCount = count($randArray); 
  16. $num = intval($param); 
  17. $resultStr = ""; 
  18. for($i = 0 ; $i < $num ; $i++){ 
  19. $resultStr .= $randArray[rand(0, intval($randCount) - 1)]; 
  20. } 
  21. return $resultStr; 
  22. } 
  23.  
  24. //随机数(数字类型) 
  25. static public function Randnum($param = 8){ 
  26. $randArray = str_split("1234567890"); 
  27. $resultStr = C::Random($randArray,$param); 
  28. return $resultStr; 
  29. } 
  30.  
  31. //随机数(混合类型) - 无0 
  32. static public function RandStr($param = 8 , $capslock = FALSE){ 
  33. $randArray = str_split("abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"); 
  34. $resultStr = C::Random($randArray,$param); 
  35. if($capslock){ 
  36. return strtoupper($resultStr); 
  37. } 
  38. else { 
  39. return $resultStr; 
  40. } 
  41. } 
  42.  
  43. //加密字符串 
  44. static public function EnBaseCode($data, $key = "ZCStrong"){ 
  45. $key = md5($key);//对于预设的KEY,MD5 
  46. $x = 0; 
  47. $len = strlen($data); 
  48. $l = strlen($key); 
  49. for ($i = 0; $i < $len; $i++){ 
  50. if ($x == $l){ 
  51. $x = 0; 
  52. } 
  53. $char .= $key{$x}; 
  54. $x++; 
  55. } 
  56. for ($i = 0; $i < $len; $i++){ 
  57. $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); 
  58. } 
  59. return base64_encode($str); 
  60. } 
  61.  
  62. //机密字符串 
  63. static public function DeBaseCode($data, $key = "ZCStrong"){ 
  64. $key = md5($key); 
  65. $x = 0; 
  66. $data = base64_decode($data); 
  67. $len = strlen($data); 
  68. $l = strlen($key); 
  69. for ($i = 0; $i < $len; $i++){ 
  70. if ($x == $l){ 
  71. $x = 0; 
  72. } 
  73. $char .= substr($key, $x, 1); 
  74. $x++; 
  75. } 
  76. for ($i = 0; $i < $len; $i++){ 
  77. if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){ 
  78. $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); 
  79. } 
  80. else{ 
  81. $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); 
  82. } 
  83. } 
  84. return $str; 
  85. } 
  86.  
  87. //正则手机号 /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))/d{8}$/ 
  88. static public function RegularPhone($string){ 
  89. $resultStr = preg_match("/^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))/d{8}$/",$string); 
  90. if(intval($resultStr) == 1){ 
  91. return TRUE; 
  92. } 
  93. else{ 
  94. return FALSE; 
  95. } 
  96. } 
  97.  
  98. //正则邮箱 
  99. static public function RegularEmail($string){ 
  100. $resultStr = preg_match("/^([0-9A-Za-z//-_//.]+)@([0-9a-z]+//.[a-z]{2,3}(//.[a-z]{2})?)$/i",$string); 
  101. if(intval($resultStr) == 1){ 
  102. return TRUE; 
  103. } 
  104. else{ 
  105. return FALSE; 
  106. } 
  107. } 
  108.  
  109. //正则验证身份证/(^([d]{15}|[d]{18}|[d]{17}x)$)/ 
  110. static public function RegularIdCard($string){ 
  111. $resultStr = preg_match("/(^([d]{15}|[d]{18}|[d]{17}x)$)/",$string); 
  112. if(intval($resultStr) == 1){ 
  113. return TRUE; 
  114. } 
  115. else{ 
  116. return FALSE; 
  117. } 
  118. } 
  119.  
  120. //处理字符串信息 
  121. static public function hStr($string){ 
  122. if(isset($string) && !emptyempty($string)){ 
  123. return addslashes(strip_tags($string)); 
  124. } 
  125. else{ 
  126. return ""; 
  127. } 
  128. } 
  129. } 

以上所述就是本文给大家介绍的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表