国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial php工具類之【String處置類】

php工具類之【String處置類】

Jun 13, 2016 pm 12:05 PM
function param return str string

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 需要轉(zhuǎn)換的字符串	 * @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;    }	/**	 +----------------------------------------------------------	 * 產(chǎn)生隨機字串,可用來自動生成密碼	 * 默認長度6位 字母和數(shù)字混合 支持中文	 +----------------------------------------------------------	 * @param string $len 長度	 * @param string $type 字串類型	 * 0 字母 1 數(shù)字 其它 混合	 * @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 = "們阿斯頓發(fā)生地方阿斯蒂芬阿斯蒂芬ASF地V字形陳V手勢地方過水電費v鬼地方乖寶寶雙方都 山東飯館山東飯館撒地方v睡的地方改水電費撒地方感受到法國地神風怪盜搞活動大發(fā)光火地方館地方高大發(fā)光火的反光板的廣播臺地方館別DVB大小分割撒地方 啥地方告訴對方釋放掉豆沙方糕撒地方干點啥風格白癜風鍋煽豆腐山東飯館時代復分高速鋼微軟消費稅大發(fā)光火好的銹掃畢璃寶芯爺鑒秘凈蔣鈣肩騰枯拋軌堂拌爸循誘祝勵肯酒繩窮塘燥泡袋朗喂鋁軟渠顆慣貿(mào)糞綜墻趨彼屆墨礙啟逆卸航衣孫齡嶺騙休借".$addChars;				break;			default :				// 默認去掉了容易混淆的字符oOLl和數(shù)字01,要添加請使用addChars參數(shù)				$chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;				break;		}		if($len>10 ) {//位數(shù)過長重復字符串一定次數(shù)			$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;	}	/**	 +----------------------------------------------------------	 * 生成一定數(shù)量的隨機數(shù),并且不重復	 +----------------------------------------------------------	 * @param integer $number 數(shù)量	 * @param string $len 長度	 * @param string $type 字串類型	 * 0 字母 1 數(shù)字 其它 混合	 +----------------------------------------------------------	 * @return string	 +----------------------------------------------------------	 */	static public function buildCountRand ($number,$length=4,$mode=1) {			if($mode==1 && $length<strlen($number) ) {				//不足以生成一定數(shù)量的不重復數(shù)字				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 字符格式	 *     # 表示數(shù)字 * 表示字母和數(shù)字 $ 表示字母	 * @param integer $number 生成數(shù)量	 +----------------------------------------------------------	 * @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 "*"://字母和數(shù)字混合						$strtemp   .= String::randString(1);						break;					case "#"://數(shù)字						$strtemp  .= String::randString(1,1);						break;					case "$"://大寫字母						$strtemp .=  String::randString(1,2);						break;					default://其他格式均不轉(zhuǎn)換						$strtemp .=   $char;						break;			   }			}			$str[] = $strtemp;		}		return $number==1? $strtemp : $str ;	}	/**	 +----------------------------------------------------------	 * 獲取一定范圍內(nèi)的隨機數(shù)字 位數(shù)不足補零	 +----------------------------------------------------------	 * @param integer $min 最小值	 * @param integer $max 最大值	 +----------------------------------------------------------	 * @return string	 +----------------------------------------------------------	 */	static public function randNumber ($min, $max) {		return sprintf("%0".strlen($max)."d", mt_rand($min,$max));	}    // 自動轉(zhuǎn)換字符集 支持數(shù)組轉(zhuǎn)換    static public function autoCharset($string, $from='gbk', $to='utf-8') {        $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;        $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;        if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) {            //如果編碼相同或者非字符串標量則不轉(zhuǎn)換            return $string;        }        if (is_string($string)) {            if (function_exists('mb_convert_encoding')) {                return mb_convert_encoding($string, $to, $from);            } elseif (function_exists('iconv')) {                return iconv($from, $to, $string);            } else {                return $string;            }        } elseif (is_array($string)) {            foreach ($string as $key => $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;        }    }}

?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

See all articles