PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/Inc/Lib/ORG/Util/String.class.php

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