php工具類之【String處置類】

來源:互聯網
上載者:User
php工具類之【String處理類】
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 = "們阿斯頓發生地方阿斯蒂芬阿斯蒂芬ASF地V字形陳V手勢地方過水電費v鬼地方乖寶寶雙方都 山東飯館山東飯館撒地方v睡的地方改水電費撒地方感受到法國地神風怪盜搞活動大發光火地方館地方高大發光火的反光板的廣播台地方館別DVB大小分割撒地方 啥地方告訴對方釋放掉豆沙方糕撒地方幹點啥風格白癜風鍋煽豆腐山東飯館時代複分高速鋼微軟消費稅大發光火好的鏽掃畢璃寶芯爺鑒秘淨蔣鈣肩騰枯拋軌堂拌爸循誘祝勵肯酒繩窮塘燥泡袋朗喂鋁軟渠顆慣貿糞綜牆趨彼屆墨礙啟逆卸航衣孫齡嶺騙休借".$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,'utf-8',false);}}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 $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;        }    }}

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.