Home  >  Article  >  Backend Development  >  Detailed explanation of String class in ThinkPHP framework

Detailed explanation of String class in ThinkPHP framework

jacklove
jackloveOriginal
2018-05-21 17:21:362621browse

You will encounter string in thinkphp development, and this article will explain its related knowledge in detail.

When developing a website in PHP, using the ThinkPHP framework can greatly improve our development efficiency. The ThinkPHP framework not only structures the skeleton of the entire website, but also provides a rich class library. The following mainly talks about the String class we use most in the ThinkPHP framework.

1. Generate UUID and Guid

uuid(); #{a67a8215-d4c0-e6e6-2dcb-9d6d180d8056}
keyGen(); #3a08fd59e58d4fce424659f12d5868d4

2. Check whether the characters are UTF8 encoded

isUtf8($str); #bool(true)

3. String interception, supports Chinese and other encodings, default Starting from 0, intercept the UTF-8 character set and display the ellipsis

msubstr($str, $start=0, $length, $charset=”utf-8″, $suffix=true)
$str:要截取的字符串
$start=0:开始位置,默认从0开始
$length:截取长度
$charset=”utf-8″:字符编码,默认UTF-8
$suffix=true:是否在截取后的字符后面显示省略号,默认true显示,false为不显示

4 at the end. Generate a random string of a specific length. The default length is 6 characters mixed with letters, numbers and Chinese. It can be used to automatically generate passwords or Verification code

randString($len=6,$type=”,$addChars=”)
$len:长度,默认6位
$type:字串类型,0字母,1数字,2大写字母或大写字母加自定义字符,3小写字母或小写字母加自定义字符,4中文或中文加自定义字符
$addChars:额外字符

5. Generate a certain number of equal-length random numbers without repetition

buildCountRand ($number,$length=4,$mode=1)
$number:数量
$len:长度,默认4位
$type:字串类型,0字母,1数字,2大写字母或大写字母加自定义字符,3小写字母或小写字母加自定义字符,4中文或中文加自定义字符

6. Generate a random character and support batch generation

buildFormatRand($format,$number=1)
$format:字符格式,#表示数字,*表示字母和数字,$表示字母
$number:生成数量,默认1个

7. Obtain Random numbers of equal length within a certain range, zeros are added if the number of digits is insufficient, and the length is the maximum length

randNumber ($min, $max)
$min:最小值
$max:最大值

8. The automatic conversion character set supports array conversion, and the default is gbk to utf-8

autoCharset($string, $from=’gbk’, $to=’utf-8′)
$string:字符串
$from:字符集,默认’gbk’,
$to:字符集,默认:’utf-8′

Before using these String class methods in the ThinkPHP controller, they need to be introduced first ($string = new \Org\Util\String in ThinkPHP3.2). If you want to use them in the page, you must copy these functions to the public function library common.php before they can be used.

This article explains the relevant knowledge of string in detail. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

JS Basics-Math Array Date

What can jQuery do?

Related knowledge points about front-end cross-domain summary

The above is the detailed content of Detailed explanation of String class in ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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