PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/firstrend/src/core/class/string.class.php

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