[String processing class] of the php tool class classString {** + UUID generation single-host usage + ----------------------------------------------- [String processing class] of the php tool class]
Class String {/** + generate UUID single-host usage + ------------------------------------------------------------ * @ access public + users * @ return string + users */static public function uuid () {$ charid = md5 (uniqid (mt_rand (), true); $ hyphen = chr (45 );/ /"-" $ Uuid = chr (123 )//"{". substr ($ charid, 0, 8 ). $ hyphen. substr ($ charid, 8, 4 ). $ hyphen. substr ($ charid, 12, 4 ). $ hyphen. substr ($ charid, 16, 4 ). $ hyphen. substr ($ charid, 20, 12 ). chr (125); // "}" return $ uuid;}/** + ---------------------------------------------------------- * generate Guid primary key + @ * @ return Boolean + -------------------------------------- ------------------ */Static public function keyGen () {return str_replace ('-', '', substr (String: uuid (), 1,-1 ));} /** + encoding * check whether the string is UTF8 encoded + encoding * @ param string $ string + encoding * @ return Boolean + ---------------------------------------- ------------------ */Static public function isUtf8 ($ str) {$ c = 0; $ B = 0; $ bits = 0; $ len = strlen ($ str ); for ($ I = 0; $ I <$ len; $ I ++) {$ c = ord ($ str [$ I]); if ($ c> 128) {if ($ c >= 254) return false; elseif ($ c >= 252) $ bits = 6; elseif ($ c >= 248) $ bits = 5; elseif ($ c >= 240) $ bits = 4; elseif ($ c >= 224) $ bits = 3; elseif ($ c >= 192) $ bits = 2; else return false; if ($ I + $ bits)> $ len) return false; while ($ bits> 1) {$ I ++; $ B = ord ($ str [$ I]); If ($ B <128 | $ B> 191) return false; $ bits --;} return true;}/** + ---------------------------------------------------------- * string truncation, supports Chinese and other encodings + ---------------------------------------------------------- * @ static * @ access public + ---------------------------------------------------------- * @ param string $ str string to be converted * @ param string $ start position * @ param string $ length truncation length * @ param string $ char Set encoding format * @ param string $ suffix truncated display characters + ------------------------------------------------------------ * @ return string + optional */static public function msubstr ($ str, $ start = 0, $ length, $ charset = "UTF-8", $ suffix = true) {if (function_exists ("mb_substr") $ slice = mb_substr ($ str, $ start, $ length, $ charset ); elseif (function_exists ('iconv _ substr') {$ sl Ice = iconv_substr ($ str, $ start, $ length, $ charset );} else {$ re ['utf-8'] = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | [\ xe0 -\ xef] [\ x80-\ xbf] {2} | [\ xf0-\ xff] [\ x80-\ xbf] {3 }/"; $ re ['gb2312'] = "/[\ x01-\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe]/"; $ re ['gbk'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] [\ x40-\ xfe]/"; $ re ['big5'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] ([\ x40-\ x7e] | \ xa1-\ xfe]) /"; preg_match_all ($ re [$ charset], $ str, $ match); $ slice = join (" ", Array_slice ($ match [0], $ start, $ length);} return $ suffix? $ Slice. '... ': $ slice;}/** + ------------------------------------------------------------ * generates random strings, it can be used to automatically generate a password * a mix of 6 letters and numbers by default supports Chinese + -------------------------------------------------- * @ param string $ len length * @ param string $ type string type * 0 Letter 1 digit and other * @ param string $ addChars extra character + ---------------------------------------------------------- * @ return string + --------------------------------- ------------------------- */Static public function randString ($ len = 6, $ type = '', $ addChars ='') {$ str = ''; switch ($ type) {case 0: $ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy '. $ addChars; break; case 1: $ chars = str_repeat ('000000', 3); break; case 2: $ chars = 'abcdefghijklmnopqrstuvwxy '. $ addChars; break; case 3: $ chars = 'abcdefghijklmnopqrstuvwxy '. $ addChars; break; case 4: $ chars = "Stephen Asti, a place in Aston Fenasf V v-shaped Chen v gesture local water and electricity fee V ghost local good baby both sides Shandong restaurant sprinkle local v sleeping place change water and electricity fee scatter place feel France's spirit of the wind and monsters engaged in activity fire the broadcasting station of the high-light reflective board of the local hall, the size of the local area, and the place where the local area tell the other party to release the bean paste, the local area, the local area, the style of the vitiligo pot, the Shandong restaurant, The Times high-speed steel, Microsoft consumption tax, high-speed steel, rust, sweep Bibao core Ye Jian mi Jing Jiang calcium shoulder Teng dry throwing rail Hall mix dad follow the temptation to liken wine rope poor pond dry bubble bag long feed aluminum soft canoes used to trade dung integrated wall tends to be another ink congestion Qi reverse unload air clothing Sun Ling lie Hugh borrow ". $ addChars; break; default: // The confusing oOLl and number 01 are removed by default. to add the parameter $ chars = 'signature', use addChars '. $ addChars; break;} if ($ len> 10) {// The string is too long to repeat for a certain number of times. $ chars = $ Type = 1? Str_repeat ($ chars, $ len): str_repeat ($ chars, 5);} if ($ type! = 4) {$ chars = str_shuffle ($ chars); $ str = substr ($ chars, 0, $ len );} else {// random Chinese characters for ($ I = 0; $ I <$ len; $ I ++) {$ str. = self: msubstr ($ chars, floor (mt_rand (0, mb_strlen ($ chars, 'utf-8')-1), 1, 'utf-8 ', false);} return $ str;}/** + ------------------------------------------------------------ * generate a certain number of random numbers, and does not repeat + limit * @ param integer $ number quantity * @ param string $ len length * @ param string $ type string type * 0 Letter 1 digit other mixed + ------------------------------------------------ * @ return string + ---------------------------------------------------------- */static public function buildCountRand ($ number, $ length = 4, $ mode = 1) {if ($ mode = 1 & $ length
$ Val) {$ _ key = self: autoCharset ($ key, $ from, $ to); $ string [$ _ key] = self: autoCharset ($ val, $ from, $ to); if ($ key! =$ _ Key) unset ($ string [$ key]);} return $ string;} else {return $ string ;}}}
?