PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/ThinkPHP/Lib/ORG/Util/String.class.php

http://iiccms.googlecode.com/
PHP | 257 lines | 146 code | 10 blank | 101 comment | 19 complexity | 47ffb9d6e0bb55a05ebdb2f05d8814aa 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. {
  98. if(function_exists("mb_substr"))
  99. return mb_substr($str, $start, $length, $charset);
  100. elseif(function_exists('iconv_substr')) {
  101. return iconv_substr($str,$start,$length,$charset);
  102. }
  103. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  104. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  105. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  106. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  107. preg_match_all($re[$charset], $str, $match);
  108. $slice = join("",array_slice($match[0], $start, $length));
  109. if($suffix) return $slice."…";
  110. return $slice;
  111. }
  112. /**
  113. +----------------------------------------------------------
  114. * ????????????????
  115. * ????6? ??????? ????
  116. +----------------------------------------------------------
  117. * @param string $len ??
  118. * @param string $type ????
  119. * 0 ?? 1 ?? ?? ??
  120. * @param string $addChars ????
  121. +----------------------------------------------------------
  122. * @return string
  123. +----------------------------------------------------------
  124. */
  125. static public function rand_string($len=6,$type='',$addChars='') {
  126. $str ='';
  127. switch($type) {
  128. case 0:
  129. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
  130. break;
  131. case 1:
  132. $chars= str_repeat('0123456789',3);
  133. break;
  134. case 2:
  135. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
  136. break;
  137. case 3:
  138. $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
  139. break;
  140. case 4:
  141. $chars = "????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????".$addChars;
  142. break;
  143. default :
  144. // ????????????oOLl???01???????addChars??
  145. $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
  146. break;
  147. }
  148. if($len>10 ) {//?????????????
  149. $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
  150. }
  151. if($type!=4) {
  152. $chars = str_shuffle($chars);
  153. $str = substr($chars,0,$len);
  154. }else{
  155. // ?????
  156. for($i=0;$i<$len;$i++){
  157. $str.= self::msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
  158. }
  159. }
  160. return $str;
  161. }
  162. /**
  163. +----------------------------------------------------------
  164. * ????????????????
  165. +----------------------------------------------------------
  166. * @param integer $number ??
  167. * @param string $len ??
  168. * @param string $type ????
  169. * 0 ?? 1 ?? ?? ??
  170. +----------------------------------------------------------
  171. * @return string
  172. +----------------------------------------------------------
  173. */
  174. static public function build_count_rand ($number,$length=4,$mode=1) {
  175. if($mode==1 && $length<strlen($number) ) {
  176. //???????????????
  177. return false;
  178. }
  179. $rand = array();
  180. for($i=0; $i<$number; $i++) {
  181. $rand[] = rand_string($length,$mode);
  182. }
  183. $unqiue = array_unique($rand);
  184. if(count($unqiue)==count($rand)) {
  185. return $rand;
  186. }
  187. $count = count($rand)-count($unqiue);
  188. for($i=0; $i<$count*3; $i++) {
  189. $rand[] = rand_string($length,$mode);
  190. }
  191. $rand = array_slice(array_unique ($rand),0,$number);
  192. return $rand;
  193. }
  194. /**
  195. +----------------------------------------------------------
  196. * ????????? ??????
  197. * ???????
  198. +----------------------------------------------------------
  199. * @param string $format ????
  200. * # ???? * ??????? $ ????
  201. * @param integer $number ????
  202. +----------------------------------------------------------
  203. * @return string | array
  204. +----------------------------------------------------------
  205. */
  206. static public function build_format_rand($format,$number=1)
  207. {
  208. $str = array();
  209. $length = strlen($format);
  210. for($j=0; $j<$number; $j++) {
  211. $strtemp = '';
  212. for($i=0; $i<$length; $i++) {
  213. $char = substr($format,$i,1);
  214. switch($char){
  215. case "*"://???????
  216. $strtemp .= String::rand_string(1);
  217. break;
  218. case "#"://??
  219. $strtemp .= String::rand_string(1,1);
  220. break;
  221. case "$"://????
  222. $strtemp .= String::rand_string(1,2);
  223. break;
  224. default://????????
  225. $strtemp .= $char;
  226. break;
  227. }
  228. }
  229. $str[] = $strtemp;
  230. }
  231. return $number==1? $strtemp : $str ;
  232. }
  233. /**
  234. +----------------------------------------------------------
  235. * ???????????? ??????
  236. +----------------------------------------------------------
  237. * @param integer $min ???
  238. * @param integer $max ???
  239. +----------------------------------------------------------
  240. * @return string
  241. +----------------------------------------------------------
  242. */
  243. static public function rand_number ($min, $max) {
  244. return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
  245. }
  246. }
  247. ?>