PHP string related operation methods
- find character position
-
- strlen() Get the length of the string
- mb_strlen() Get the length of strings such as Chinese
- strpos() Returns the position (case-sensitive) of the first occurrence of a string within another string.
- stripos() Returns the position of the first occurrence of a string within another string (case-insensitive).
- strrpos() Finds the last occurrence of a string within another string (case sensitive).
- strripos() Finds the last occurrence of a string within another string (case insensitive).
- strstr() Searches whether a string exists in another string, if yes, returns the string and the rest, otherwise returns FALSE. (case sensitive)
- stristr() Searches for the presence of a string in another string, and if so, returns that string and the remainder, otherwise returns FALSE. (not case sensitive)
- The substr() function returns a part of a string
- The mb_substr() function returns a part of a Chinese character string
- substr_count() counts the number of times a substring occurs in a string.
- substr_replace() replaces part of a string with another string.
- Array to string conversion
- case conversion
- Clear spaces or specified characters
- encryption
- string replacement
- other
concatenation operator
The concatenation operator (.) is used to concatenate two string values.
$a = 'hello';
$b = 'world!'
echo $a.' '.$b;// hello world
find character position
strlen() Get the length of the string
$a = 'hello';
echo strlen($a);// 5
mb_strlen() Get the length of strings such as Chinese
When it is different from strlen, it can return the corresponding number of characters by setting the character encoding, which handles the length of the Chinese string well.
Use mb_strlen to enable the mbstring extension.
grammar
strlen(string[, string $encoding = mb_internal_encoding() ] ))
mb_internal_encoding() is used to set/get internal character encoding.
parameter | describe |
---|---|
string | required. Specifies the string to check. |
encoding | optional. Character Encoding. If omitted, the internal character encoding is used. |
$a = '你好';
echo mb_strlen($a);// 2
echo strlen($a);// 6
strpos() Returns the position (case-sensitive) of the first occurrence of a string within another string.
If a match is found in the string, the function returns the character position of the first match. Returns FALSE if no match is found.
grammar
strpos(string,find,start)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
find | required. Specifies the character to look for. |
start | optional. Specifies where to start the search. |
$a = 'hello world!';
echo strpos($a,'world');// 6
stripos() Returns the position of the first occurrence of a string within another string (case-insensitive).
If a match is found in the string, the function returns the character position of the first match. Returns FALSE if no match is found.
grammar
stripos(string,find,start)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
find | required. Specifies the character to look for. |
start | optional. Specifies where to start the search. |
$a = 'hello world!';
echo stripos($a,'World');// 6
strrpos() Finds the last occurrence of a string within another string (case sensitive).
If a match is found in the string, the function returns the character position of the last match. Returns FALSE if no match is found.
grammar
strrpos(string,find,start)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
find | required. Specifies the character to look for. |
start | optional. Specifies where to start the search. |
$a = 'hello world!';
echo strrpos($a,'world');// 6
strripos() Finds the last occurrence of a string within another string (case insensitive).
If a match is found in the string, the function returns the character position of the last match. Returns FALSE if no match is found.
grammar
strripos(string,find,start)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
find | required. Specifies the character to look for. |
start | optional. Specifies where to start the search. |
$a = 'hello world!';
echo strripos($a,'World');// 6
strstr() Searches whether a string exists in another string, if yes, returns the string and the rest, otherwise returns FALSE. (case sensitive)
grammar
strstr(string,search,before_search)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
search | required. Specifies the string to search for. If the argument is a number, searches for characters that match the ASCII value for the number. |
before_search | optional. A boolean with a default value of "false". If set to "true", it will return the portion of the string up to the first occurrence of the search parameter. |
$a = 'hello world!';
echo strstr($a,'world');// world!
stristr() Searches for the presence of a string in another string, and if so, returns that string and the remainder, otherwise returns FALSE. (not case sensitive)
grammar
stristr(string,search,before_search)
parameter | describe |
---|---|
string | required. Specifies the string to be searched. |
search | required. Specifies the string to search for. If the argument is a number, searches for characters that match the ASCII value for the number. |
before_search | optional. A boolean with a default value of "false". If set to "true", it will return the portion of the string up to the first occurrence of the search parameter. |
$a = 'hello world!';
echo stristr($a,'World');// world!
The substr() function returns a part of a string
Note: If the start parameter is negative and length is less than or equal to start, then length is 0.
grammar
substr(string,start,length)
parameter | describe |
---|---|
string | required. Specifies the string to return part of. |
start | required. Specifies where in the string to start. Positive numbers - start at the specified position in the string Negative numbers - start at the specified position from the end of the string 0 - start at the first character in the string |
length | optional. Specifies the length of the string to return. The default is until the end of the string. Positive numbers - return from where the start parameter is located Negative numbers - return from the end of the string |
echo substr("Hello world",6);// world
The mb_substr() function returns a part of a Chinese character string
Note: If the start parameter is negative and length is less than or equal to start, then length is 0.
grammar
mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string
parameter | describe |
---|---|
string | required. Specifies the string to return part of. |
start | required. Specifies where in the string to start. Positive numbers - start at the specified position in the string Negative numbers - start at the specified position from the end of the string 0 - start at the first character in the string |
length | optional. Specifies the length of the string to return. The default is until the end of the string. Positive numbers - return from where the start parameter is located Negative numbers - return from the end of the string |
encoding | optional. Character Encoding. If omitted, the internal character encoding is used. |
echo mb_substr("世界你好", 0, 2);// 世界
echo substr("世界你好", 0, 2);// �
substr_count() 计算子串在字符串中出现的次数。
注释:子串是区分大小写的。
注释:该函数不计数重叠的子串
注释:如果 start 参数加上 length 参数大于字符串长度,该函数则生成一个警告
substr_count(string,substring,start,length)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
substring | 必需。规定要检索的字符串。 |
start | 可选。规定在字符串中何处开始搜索。 |
length | 可选。规定搜索的长度。 |
echo substr_count("Hello world. The world is nice","world");// 2
substr_replace() 把字符串的一部分替换为另一个字符串。
如果 start 参数是负数且 length 小于或者等于 start,则 length 为 0
substr_replace(string,replacement,start,length)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
replacement | 必需。规定要插入的字符串。 |
start | 必需。规定在字符串的何处开始替换。 正数 - 在字符串的指定位置开始 负数 - 在从字符串结尾的指定位置开始 0 - 在字符串中的第一个字符处开始 |
length | 可选。规定要替换多少个字符。默认是与字符串长度相同。 正数 - 被替换的字符串长度 负数 - 从字符串末端开始的被替换字符数 0 - 插入而非替换 |
echo substr_replace("Hello","world",0);// world
数组字符串转换
join() 数组转字符串
join() 函数是 implode() 函数的别名。
语法
join(separator,array)
参数 | 描述 |
---|---|
separator | 可选。规定数组元素之间放置的内容。默认是 “”(空字符串)。 |
array | 必需。要组合为字符串的数组。 |
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);// Hello World! Beautiful Day!
explode() 字符串切割为数组
语法
explode(separator,string,limit)
参数 | 描述 |
---|---|
separator | 必需。规定在哪里分割字符串。 |
string | 必需。要分割的字符串。 |
limit | 可选。规定所返回的数组元素的数目。 可能的值: 大于 0 - 返回包含最多 limit 个元素的数组 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组 0 - 会被当做 1, 返回包含一个元素的数组 |
$str = "1.2.3";
print_r (explode(".",$str));// Array ( [0] => 1 [1] => 2 [2] => 3 )
大小写转换
lcfirst() 字符串首字符转换为小写
lcfirst() 函数把字符串中的首字符转换为小写。
echo lcfirst("Hello world!");// hello world!
ucfirst() 字符串首字符转换为大写
ucfirst() 函数把字符串中的首字符转换为大写。
echo ucfirst("hello world!");// Hello world!
ucwords() 每个单词的首字符转换为大写
ucwords() 函数把每个单词的首字符转换为大写。
echo ucwords("hello world!");// Hello World!
strtoupper() 把所有字符转换为大写
strtoupper() 函数把所有字符转换为大写。
echo strtoupper("hello world!");// HELLO WORLD!
strtolower() 把所有字符转换为小写
strtolower() 函数把所有字符转换为小写。
echo strtolower("HELLO WORLD!");// hello world!
清除空格或指定字符
注意此处是字符,只要左右包含预设值中的字符就会被删除
rtrim() 移除字符串右侧的空白字符或其他预定义字符
$str = "Hello World!";
echo rtrim($str,"World!");// Hello (注意还有一个空格)
语法
rtrim(string,charlist)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符: “\0” - NULL “\t” - 制表符 “\n” - 换行 “\x0B” - 垂直制表符 “\r” - 回车 " " - 空格 |
ltrim() 移除字符串左侧的空白字符或其他预定义字符
$str = "Hello World!";
echo '-'. ltrim($str,"Hello");// - World!
语法
ltrim(string,charlist)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符: “\0” - NULL “\t” - 制表符 “\n” - 换行 “\x0B” - 垂直制表符 “\r” - 回车 " " - 空格 |
trim() 移除字符串左右两侧的空白字符或其他预定义字符
$str = "Hello World!";
echo trim($str,"Hed!");// llo Worl
语法
trim(string,charlist)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符: “\0” - NULL “\t” - 制表符 “\n” - 换行 “\x0B” - 垂直制表符 “\r” - 回车 " " - 空格 |
加密
sha1() 计算字符串的 SHA-1 散列
$str = "Hello";
echo sha1($str);// f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
语法
sha1(string,raw)
参数 | 描述 |
---|---|
string | 必需。规定要计算的字符串。 |
raw | 可选。规定十六进制或二进制输出格式: TRUE - 原始 20 字符二进制格式 FALSE - 默认。40 字符十六进制数 |
md5() 计算字符串的 MD5 散列
$str = "Hello";
echo md5($str);// 8b1a9953c4611296a827abf8c47804d7
语法
md5(string,raw)
参数 | 描述 |
---|---|
string | 必需。规定要计算的字符串。 |
raw | 可选。规定十六进制或二进制输出格式: TRUE - 原始 16 字符二进制格式 FALSE - 默认。32 字符十六进制数 |
字符串替换
str_replace() 替换字符串中的一些字符(区分大小写)。
该函数必须遵循下列规则:
- 如果搜索的字符串是一个数组,那么它将返回一个数组。
- 如果搜索的字符串是一个数组,那么它将对数组中的每个元素进行查找和替换。
- 如果同时需要对某个数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余的元素将用空字符串进行替换。
- 如果是对一个数组进行查找,但只对一个字符串进行替换,那么替代字符串将对所有查找到的值起作用。
语法
str_replace(find,replace,string,count)
参数 | 描述 |
---|---|
find | 必需。规定要查找的值。 |
replace | 必需。规定替换 find 中的值的值。 |
string | 必需。规定被搜索的字符串。 |
count | 可选。一个变量,对替换数进行计数。 |
str_ireplace() 替换字符串中的一些字符(不区分大小写)。
该函数必须遵循下列规则:
- 如果搜索的字符串是一个数组,那么它将返回一个数组。
- 如果搜索的字符串是一个数组,那么它将对数组中的每个元素进行查找和替换。
- 如果同时需要对某个数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余的元素将用空字符串进行替换。
- 如果是对一个数组进行查找,但只对一个字符串进行替换,那么替代字符串将对所有查找到的值起作用。
语法
str_ireplace(find,replace,string,count)
参数 | 描述 |
---|---|
find | 必需。规定要查找的值。 |
replace | 必需。规定替换 find 中的值的值。 |
string | 必需。规定被搜索的字符串。 |
count | 可选。一个变量,对替换数进行计数。 |
其他
strrev() 反转字符串。
echo strrev("Hello World!");// !dlroW olleH