strlen()函数
strlen() 函数返回字符串的长度,以字符计。
常用于循环和其他函数,在确定字符串何时结束很重要时。(例如,在循环中,我们也许需要在字符串的最后一个字符之后停止循环)。
语法:
strlen(string)
string:必需,规定要检查的字符串。
返回值:若成功则返回字符串的长度,若字符串为空则返回0。
<?php
echo strlen("Hello world!");
?>
输出:12
str_word_count() 函数
str_word_count() 函数对字符串中的单词进行计数。
语法:
str_word_count(string,return,char)
string:必需,规定要检查的字符串。
return:可选,规定 str_word_count() 函数的返回值。可能的值:
0 - 默认,返回找到的单词的数目。
1 - 返回包含字符串中的单词的数组。
2 - 返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词。
char:可选,规定被视为单词的特殊字符。
返回值:返回数字或者数组,取决于所选择的 return 参数。
<?php
echo str_word_count("Hello world!");
?>
输出:2
strrev() 函数
strrev() 函数反转字符串。
语法:
strrev(string)
string:必需规定要反转的字符串。
返回值:返回已反转的字符串。
<?php
echo strrev("Hello world!");
?>
输出:!dlrow olleH
strpos() 函数(大小写敏感)
strpos() 函数用于检索字符串内指定的字符或文本。
语法:
strpos(string,find,start)
string:必需,规定要搜索的字符串。
find:必需,规定要查找的字符串。
start:可选,规定在何处开始搜索。
返回值:返回字符串在另一字符串中第一次出现的位置,如果没有找到字符串则返回FALSE。
<?php
echo strpos("Hello world!","world"); //字符串中首字符的位置是 0 而不是 1
?>
输出:6
stripos() 函数(大小写不敏感)
stripos() 函数查找字符串在另一字符串中第一次出现的位置。
语法:
stripos(string,find,start
string:必需,规定要搜索的字符串。
find:必需,规定要查找的字符。
start:可选,规定开始搜索的位置。
<?php
echo stripos("Hello,welcome to my home!","To");
?>
输出:14
strrpos() 函数(大小写敏感)
strrpos() 函数查找字符串在另一字符串中最后一次出现的位置。
语法:
strrpos(string,find,start)
string:必需,规定被搜索的字符串。
find:必需,规定要查找的字符。
start:可选,规定在何处开始搜索。
返回值:返回字符串在另一字符串中最后一次出现的位置,如果没有找到字符串则返回FALSE。
<?php
echo stripos("Hello,welcome to my home!I'm glad to see you.","to");
?>
输出:34
strripos() 函数(大小写不敏感)
strripos() 函数查找字符串在另一字符串中最后一次出现的位置。
语法:
strripos(string,find,start)
string:必需,规定被搜索的字符串。
find:必需,规定要查找的字符。
start:可选,规定在何处开始搜索。
返回值:返回字符串在另一字符串中最后一次出现的位置,如果没有找到字符串则返回FALSE。
<?php
echo stripos("Hello,welcome to my home!I'm glad to see you.","TO");
?>
输出:34
str_replace() 函数(对大小写敏感)
str_replace() 函数用一些字符串替换字符串中的另一些字符。
语法:
str_replace(find,replace,string,count)
find:必需,规定要查找的值。
replace:必需,规定替换 find 中的值的值。
string:必需,规定被搜索的字符串。
count:可选,对替换数进行计数的变量。
返回值: 返回带有替换值的字符串或数组。
<?php
echo str_replace("world", "Kitty", "Hello world!");
?>
输出:Hello Kitty!
str_ireplace() 函数(对大小写不敏感)
str_ireplace() 函数替换字符串中的一些字符。
语法:
str_ireplace(find,replace,string,count)
find:必需,规定要查找的值。
replace:必需,规定替换 find 中的值的值。
string:必需,规定被搜索的字符串。
count:可选,对替换数进行计数的变量。
返回值: 返回带有替换值的字符串或数组。
<?php
echo str_ireplace("WORLD","Shanghai","Hello world!");
?>
输出:Hello Shanghai!
lcfirst() 函数
lcfirst() 函数把字符串中的首字符转换为小写
<?php
echo lcfirst("Hello world!");
?>
输出:hello world!
ucfirst() 函数
ucfirst() 函数把字符串中的首字符转换为大写。
<?php
echo ucfirst("hello world!");
?>
输出:Hello world!
ucwords() 函数
ucwords() 函数把字符串中每个单词的首字符转换为大写,该函数是二进制安全的。
<?php
echo ucwords("hello world");
?>
输出:Hello World!
strtolower() 函数
strtolower() 函数把字符串转换为小写,该函数是二进制安全的。
<?php
echo strtolower("Hello WORLD!");
?>
输出:hello world!
strtoupper() 函数
strtoupper() 函数把字符串转换为大写,该函数是二进制安全的。
<?php
echo strtoupper("Hello WORLD!");
?>
输出:HELLO WORLD!
str_pad() 函数
str_pad() 函数把字符串填充为新的长度。
语法:
str_pad(string,length,pad_string,pad_type)
string:必需,规定要填充的字符串。
length:必需,规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。
pad_string:可选,规定供填充使用的字符串,默认是空白。
pad_type:可选,规定填充字符串的哪边。可能的值:
STR_PAD_BOTH - 填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。
STR_PAD_LEFT - 填充字符串的左侧。
STR_PAD_RIGHT - 填充字符串的右侧,默认。
填充字符串的右侧,到 30 个字符的新长度:
<?php
$str = "Hello World";
echo str_pad($str,30,".");
?>
输出:Hello World...................
str_repeat() 函数
str_repeat() 函数把字符串重复指定的次数。
语法:
str_repeat(string,repeat)
string:必需,规定要重复的字符串。
repeat:必需,规定字符串将被重复的次数,必须大于等于0。
<?php
echo str_repeat("Hello ",5);
?>
输出:Hello Hello Hello Hello Hello
str_split() 函数
str_split() 函数把字符串分割到数组中。
语法
str_split(string,length)
string:必需,规定要分割的字符串。
length:可选,规定每个数组元素的长度,默认是1。
返回值:
如果 length 小于 1,则 str_split() 函数将返回 FALSE。
如果 length 大于字符串的长度,则整个字符串将作为数组的唯一元素返回。
<?php
print_r(str_split("Chengdu",3));
?>
输出:Array ( [0] => Che [1] => ngd [2] => u )
substr() 函数
substr() 函数返回字符串的一部分。
如果 start 参数是负数且 length 小于或等于 start,则 length 为 0。
语法:
substr(string,start,length)
string:必需,规定要返回其中一部分的字符串。
start:必需,规定在字符串的何处开始。
正数 - 在字符串的指定位置开始
负数 - 在从字符串结尾开始的指定位置开始
0 - 在字符串中的第一个字符处开始
length:可选,规定被返回字符串的长度。默认是直到字符串的结尾。
正数 - 从 start 参数所在的位置返回的长度
负数 - 从字符串末端返回的长度
返回值:返回字符串的提取部分,若失败则返回 FALSE,或者返回一个空字符串。
<?php
echo substr("Hello world",6);
?>
输出:world
substr_count() 函数
substr_count() 函数计算子串在字符串中出现的次数。
子串是区分大小写的。
该函数不计数重叠的子串。
如果 start 参数加上 length 参数大于字符串长度,则该函数生成一个警告。
语法:
substr_count(string,substring,start,length)
string:必需,规定被检查的字符串。
substring:必需,规定要搜索的字符串。
start:可选,规定在字符串中何处开始搜索。
length:可选,规定搜索的长度。
返回值:返回子串在字符串中出现的次数。
<?php
echo substr_count("Hello,welcome to my home!I'm glad to see you.","to");
?>
输出:2