PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Examples/Rbac/Lib/ORG/Util/String.class.php

http://thinkphp.googlecode.com/
PHP | 252 lines | 141 code | 10 blank | 101 comment | 18 complexity | 28b59f30304c38aeb1ee2675ab09d6ab MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: String.class.php 2601 2012-01-15 04:59:14Z liu21st $
  12. class String {
  13. /**
  14. +----------------------------------------------------------
  15. * ??UUID ????
  16. +----------------------------------------------------------
  17. * @access public
  18. +----------------------------------------------------------
  19. * @return string
  20. +----------------------------------------------------------
  21. */
  22. static public function uuid() {
  23. $charid = md5(uniqid(mt_rand(), true));
  24. $hyphen = chr(45);// "-"
  25. $uuid = chr(123)// "{"
  26. .substr($charid, 0, 8).$hyphen
  27. .substr($charid, 8, 4).$hyphen
  28. .substr($charid,12, 4).$hyphen
  29. .substr($charid,16, 4).$hyphen
  30. .substr($charid,20,12)
  31. .chr(125);// "}"
  32. return $uuid;
  33. }
  34. /**
  35. +----------------------------------------------------------
  36. * ??Guid??
  37. +----------------------------------------------------------
  38. * @return Boolean
  39. +----------------------------------------------------------
  40. */
  41. static public function keyGen() {
  42. return str_replace('-','',substr(String::uuid(),1,-1));
  43. }
  44. /**
  45. +----------------------------------------------------------
  46. * ????????UTF8??
  47. +----------------------------------------------------------
  48. * @param string $string ???
  49. +----------------------------------------------------------
  50. * @return Boolean
  51. +----------------------------------------------------------
  52. */
  53. static public function isUtf8($str) {
  54. $c=0; $b=0;
  55. $bits=0;
  56. $len=strlen($str);
  57. for($i=0; $i<$len; $i++){
  58. $c=ord($str[$i]);
  59. if($c > 128){
  60. if(($c >= 254)) return false;
  61. elseif($c >= 252) $bits=6;
  62. elseif($c >= 248) $bits=5;
  63. elseif($c >= 240) $bits=4;
  64. elseif($c >= 224) $bits=3;
  65. elseif($c >= 192) $bits=2;
  66. else return false;
  67. if(($i+$bits) > $len) return false;
  68. while($bits > 1){
  69. $i++;
  70. $b=ord($str[$i]);
  71. if($b < 128 || $b > 191) return false;
  72. $bits--;
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. /**
  79. +----------------------------------------------------------
  80. * ???????????????
  81. +----------------------------------------------------------
  82. * @static
  83. * @access public
  84. +----------------------------------------------------------
  85. * @param string $str ????????
  86. * @param string $start ????
  87. * @param string $length ????
  88. * @param string $charset ????
  89. * @param string $suffix ??????
  90. +----------------------------------------------------------
  91. * @return string
  92. +----------------------------------------------------------
  93. */
  94. static public function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
  95. if(function_exists("mb_substr"))
  96. $slice = mb_substr($str, $start, $length, $charset);
  97. elseif(function_exists('iconv_substr')) {
  98. $slice = iconv_substr($str,$start,$length,$charset);
  99. }else{
  100. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  101. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  102. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  103. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  104. preg_match_all($re[$charset], $str, $match);
  105. $slice = join("",array_slice($match[0], $start, $length));
  106. }
  107. return $suffix ? $slice.'...' : $slice;
  108. }
  109. /**
  110. +----------------------------------------------------------
  111. * ????????????????
  112. * ????6? ??????? ????
  113. +----------------------------------------------------------
  114. * @param string $len ??
  115. * @param string $type ????
  116. * 0 ?? 1 ?? ?? ??
  117. * @param string $addChars ????
  118. +----------------------------------------------------------
  119. * @return string
  120. +----------------------------------------------------------
  121. */
  122. static public function randString($len=6,$type='',$addChars='') {
  123. $str ='';
  124. switch($type) {
  125. case 0:
  126. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
  127. break;
  128. case 1:
  129. $chars= str_repeat('0123456789',3);
  130. break;
  131. case 2:
  132. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
  133. break;
  134. case 3:
  135. $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
  136. break;
  137. case 4:
  138. $chars = "????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????".$addChars;
  139. break;
  140. default :
  141. // ????????????oOLl???01???????addChars??
  142. $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
  143. break;
  144. }
  145. if($len>10 ) {//?????????????
  146. $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
  147. }
  148. if($type!=4) {
  149. $chars = str_shuffle($chars);
  150. $str = substr($chars,0,$len);
  151. }else{
  152. // ?????
  153. for($i=0;$i<$len;$i++){
  154. $str.= self::msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
  155. }
  156. }
  157. return $str;
  158. }
  159. /**
  160. +----------------------------------------------------------
  161. * ????????????????
  162. +----------------------------------------------------------
  163. * @param integer $number ??
  164. * @param string $len ??
  165. * @param string $type ????
  166. * 0 ?? 1 ?? ?? ??
  167. +----------------------------------------------------------
  168. * @return string
  169. +----------------------------------------------------------
  170. */
  171. static public function buildCountRand ($number,$length=4,$mode=1) {
  172. if($mode==1 && $length<strlen($number) ) {
  173. //???????????????
  174. return false;
  175. }
  176. $rand = array();
  177. for($i=0; $i<$number; $i++) {
  178. $rand[] = self::randString($length,$mode);
  179. }
  180. $unqiue = array_unique($rand);
  181. if(count($unqiue)==count($rand)) {
  182. return $rand;
  183. }
  184. $count = count($rand)-count($unqiue);
  185. for($i=0; $i<$count*3; $i++) {
  186. $rand[] = self::randString($length,$mode);
  187. }
  188. $rand = array_slice(array_unique ($rand),0,$number);
  189. return $rand;
  190. }
  191. /**
  192. +----------------------------------------------------------
  193. * ????????? ??????
  194. * ???????
  195. +----------------------------------------------------------
  196. * @param string $format ????
  197. * # ???? * ??????? $ ????
  198. * @param integer $number ????
  199. +----------------------------------------------------------
  200. * @return string | array
  201. +----------------------------------------------------------
  202. */
  203. static public function buildFormatRand($format,$number=1) {
  204. $str = array();
  205. $length = strlen($format);
  206. for($j=0; $j<$number; $j++) {
  207. $strtemp = '';
  208. for($i=0; $i<$length; $i++) {
  209. $char = substr($format,$i,1);
  210. switch($char){
  211. case "*"://???????
  212. $strtemp .= String::randString(1);
  213. break;
  214. case "#"://??
  215. $strtemp .= String::randString(1,1);
  216. break;
  217. case "$"://????
  218. $strtemp .= String::randString(1,2);
  219. break;
  220. default://????????
  221. $strtemp .= $char;
  222. break;
  223. }
  224. }
  225. $str[] = $strtemp;
  226. }
  227. return $number==1? $strtemp : $str ;
  228. }
  229. /**
  230. +----------------------------------------------------------
  231. * ???????????? ??????
  232. +----------------------------------------------------------
  233. * @param integer $min ???
  234. * @param integer $max ???
  235. +----------------------------------------------------------
  236. * @return string
  237. +----------------------------------------------------------
  238. */
  239. static public function randNumber ($min, $max) {
  240. return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
  241. }
  242. }