1、chop()
从字符串右端移除字符
chop(string,charlist)
$str="hello world~"; echo chop($str,"ld~"); //hello wor
ps. charlist参数是可选的,如果不填,默认移除
- "\0" - NULL
- "\t" - 制表符
- "\n" - 换行
- "\x0B" - 垂直制表符
- "\r" - 回车
- " " - 空格
2、chunk_split()
把字符串分割为一连串更小的部分。
chunk_split(string,length,end)
$str="hello world~"; echo chunk_split($str,1,"."); //h.e.l.l.o. .w.o.r.l.d.~.
3、explode()
把字符串打散为数组。
explode(separator,string,limit)
$str="beijing,shanghai,nanjing,tianjing,anqing"; print_r(explode(",",$str,3)); //Array ( [0] => beijing [1] => shanghai [2] => nanjing,tianjing,anqing )
ps. limit可选,规定所返回的数组元素的数目。
4、htmlspecialchars()
把预定义的字符转换为 HTML 实体。
htmlspecialchars(string,flags,character-set,double_encode)
$str="hello <em>world</em>~"; echo htmlspecialchars($str); //hello <em>world</em>~
- & (和号)成为 &
- " (双引号)成为 "
- ' (单引号)成为 '
- < (小于)成为 <
- > (大于)成为 >
5、implode() //别名为join()
返回由数组元素组合成的字符串。
implode(separator,array)
$arr=array("shanghai,nanjing,beijing,tianjing,anqing"); echo implode(" ",$arr); //shanghai,nanjing,beijing,tianjing,anqing
ps. separator可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。
6、lcfirst()
把字符串中的首字符转换为小写。
lcfirst(string)
$arr="Hello world"; echo lcfirst($arr); //hello world
7、trim() //ltrim 移除左侧 rtrim移除右侧
移除字符串两侧的字符。
trim(string,charlist)
$str=" hello world "; echo strlen(trim($str,"ld ")); //9
ps. charlist
可选。规定从字符串中删除哪些字符。如果被省略,则移除以下所有字符:
- "\0" - NULL
- "\t" - 制表符
- "\n" - 换行
- "\x0B" - 垂直制表符
- "\r" - 回车
- " " - 空格
8、number_format()
通过千位分组来格式化数字。
number_format(number,decimals,decimalpoint,separator)
$arr=5000000000; echo number_format($arr,2); //5,000,000,000.00
ps. decimals可选。规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字。
9、print()
输出一个或多个字符串。
print(strings)
$arr="hello world"; print $arr; //hello world
ps. print() 函数实际不是一个函数,所以您不必对它使用括号。print() 函数比 echo() 稍慢。
10、str_ireplace() //str_replace() 对大小写敏感
替换字符串中的一些字符(不区分大小写)。
str_ireplace(find,replace,string,count)
$str="hello world"; echo str_ireplace("world","shanghai",$str); //hello shanghai
11、str_pad()
把字符串填充为新的长度。
str_pad(string,length,pad_string,pad_type)
$str="hello world"; echo str_pad($str,30,"~"); //hello world~~~~~~~~~~~~~~~~~~~
ps. pad_string可选。规定供填充使用的字符串。默认是空白。
pad_type可选。规定填充字符串的哪边。
可能的值:
- STR_PAD_BOTH - 填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。
- STR_PAD_LEFT - 填充字符串的左侧。
- STR_PAD_RIGHT - 填充字符串的右侧。默认。
12、str_repeat()
把字符串重复指定的次数
str_repeat(string,repeat)
$str="hello "; echo str_repeat($str,3); //hello hello hello
13、str_shuffle()
随机打乱字符串中的所有字符
str_shuffle(string)
$str="hello world"; echo str_shuffle($str); //owde olhlrl
14、str_split()
把字符串分割到数组中
str_split(string,length)
$str="hello world"; print_r(str_split($str,3)); //Array ( [0] => hel [1] => lo [2] => wor [3] => ld )
15、str_word_count()
计算字符串中的单词数
str_word_count(string,return,char)
$str="hello world"; echo str_word_count($str); //2
ps. return参数可选,
可能的值:
- 0 - 默认。返回找到的单词的数目。
- 1 - 返回包含字符串中的单词的数组。
- 2 - 返回一个数组,其中的键名是单词在字符串中的位置,键值是实际的单词。
$str="hello world"; print_r(str_word_count($str,1)); //Array ( [0] => hello [1] => world )
16、strstr() //别名strchr
搜索字符串在另一字符串中的第一次出现,并返回字符串的剩余部分。
strstr(string,search,before_search)
$str="hello world"; echo strstr($str,"o"); //o world
before_search参数可选。默认值为 "false" 的布尔值。
如果设置为 "true",它将返回 search 参数第一次出现之前的字符串部分。
$str="hello world"; echo strstr($str,"o",true); //hell
17、str_ipos() //strpos()区分大小写 strripos()最后一次出现的位置,不区分大小写 strrpos()最后一次出现的位置,区分大小写
查找字符串在另一字符串中第一次出现的位置(不区分大小写)
stripos(string,find,start)
$str="you love php,i love php too"; echo stripos($str,"PHP"); //9
18、strlen()
返回字符串的长度
strlen(string)
$str="hello world"; echo strlen($str); //11
19、strrev()
反转字符串
strrev(string)
$str="hello world"; echo strrev($str); //dlrow olleh
20、strtolower() //strtoupper() 把字符串转化为大写 lcfirst() 把字符串中首字符转化为小写 ucfirst() 把字符串中的首字符转化为大写 ucwords() 把字符的每个单词首字母大写
把所有字符转化为小写
strtolower(string)
$str="HELLO WORLD"; echo strtolower($str); //hello world
21、substr()
返回字符串的一部分
substr(string,start,length)
$str="hello world"; echo substr($str,6); //world
22、substr_count()
计算子串在字符串中出现的次数
substr_count(string,substring,start,length)
$str="you love php,i love php too"; echo substr_count($str,"php"); //2
23、substr_replace()
把字符串的一部分替换成另外一个字符串
substr_replace(string,replaceement,start,length)
$str="you love php,i love php too"; echo substr_replace($str,"css",4,4); //you css php,i love php too