/Examples/Rbac/Lib/ORG/Util/String.class.php
PHP | 252 lines | 141 code | 10 blank | 101 comment | 18 complexity | 28b59f30304c38aeb1ee2675ab09d6ab MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id: String.class.php 2601 2012-01-15 04:59:14Z liu21st $
-
- class String {
-
- /**
- +----------------------------------------------------------
- * ??UUID ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- 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;
- }
-
- /**
- +----------------------------------------------------------
- * ??Guid??
- +----------------------------------------------------------
- * @return Boolean
- +----------------------------------------------------------
- */
- static public function keyGen() {
- return str_replace('-','',substr(String::uuid(),1,-1));
- }
-
- /**
- +----------------------------------------------------------
- * ????????UTF8??
- +----------------------------------------------------------
- * @param string $string ???
- +----------------------------------------------------------
- * @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;
- }
-
- /**
- +----------------------------------------------------------
- * ???????????????
- +----------------------------------------------------------
- * @static
- * @access public
- +----------------------------------------------------------
- * @param string $str ????????
- * @param string $start ????
- * @param string $length ????
- * @param string $charset ????
- * @param string $suffix ??????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- 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')) {
- $slice = 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;
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????
- * ????6? ??????? ????
- +----------------------------------------------------------
- * @param string $len ??
- * @param string $type ????
- * 0 ?? 1 ?? ?? ??
- * @param string $addChars ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- static public function randString($len=6,$type='',$addChars='') {
- $str ='';
- switch($type) {
- case 0:
- $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
- break;
- case 1:
- $chars= str_repeat('0123456789',3);
- break;
- case 2:
- $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
- break;
- case 3:
- $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
- break;
- case 4:
- $chars = "????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????".$addChars;
- break;
- default :
- // ????????????oOLl???01???????addChars??
- $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
- break;
- }
- if($len>10 ) {//?????????????
- $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
- }
- if($type!=4) {
- $chars = str_shuffle($chars);
- $str = substr($chars,0,$len);
- }else{
- // ?????
- for($i=0;$i<$len;$i++){
- $str.= self::msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
- }
- }
- return $str;
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????
- +----------------------------------------------------------
- * @param integer $number ??
- * @param string $len ??
- * @param string $type ????
- * 0 ?? 1 ?? ?? ??
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- static public function buildCountRand ($number,$length=4,$mode=1) {
- if($mode==1 && $length<strlen($number) ) {
- //???????????????
- return false;
- }
- $rand = array();
- for($i=0; $i<$number; $i++) {
- $rand[] = self::randString($length,$mode);
- }
- $unqiue = array_unique($rand);
- if(count($unqiue)==count($rand)) {
- return $rand;
- }
- $count = count($rand)-count($unqiue);
- for($i=0; $i<$count*3; $i++) {
- $rand[] = self::randString($length,$mode);
- }
- $rand = array_slice(array_unique ($rand),0,$number);
- return $rand;
- }
-
- /**
- +----------------------------------------------------------
- * ????????? ??????
- * ???????
- +----------------------------------------------------------
- * @param string $format ????
- * # ???? * ??????? $ ????
- * @param integer $number ????
- +----------------------------------------------------------
- * @return string | array
- +----------------------------------------------------------
- */
- static public function buildFormatRand($format,$number=1) {
- $str = array();
- $length = strlen($format);
- for($j=0; $j<$number; $j++) {
- $strtemp = '';
- for($i=0; $i<$length; $i++) {
- $char = substr($format,$i,1);
- switch($char){
- case "*"://???????
- $strtemp .= String::randString(1);
- break;
- case "#"://??
- $strtemp .= String::randString(1,1);
- break;
- case "$"://????
- $strtemp .= String::randString(1,2);
- break;
- default://????????
- $strtemp .= $char;
- break;
- }
- }
- $str[] = $strtemp;
- }
-
- return $number==1? $strtemp : $str ;
- }
-
- /**
- +----------------------------------------------------------
- * ???????????? ??????
- +----------------------------------------------------------
- * @param integer $min ???
- * @param integer $max ???
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- static public function randNumber ($min, $max) {
- return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
- }
- }