常用函数总结表
随机地打乱字符串中所 echo str_shuffle("Hello World"); 有字符 将字符串解析成变量
parse_str()
parse_str("id=23&name=John%20Adams 要解析的字符串|存储 ",$myArray); 变量的数组名称 print_r($myArray); 要格式化的数字|规定 多少个小数|规定用作 小数点的字符串|规定 用作千位分隔符的字符 串 目标字符串
一个包含路径的字符串
str_pad()
把字符串填充为指定的 长度 重复使用指定字符串 把字符串分割到数组中 反转字符串 按照指定长度对字符串 进行折行处理
$str = "Hello World"; echo str_pad($str,20,"."); echo str_repeat(".",13); print_r(str_split("Hello")); echo strrev("Hello World!");
字符串函数 函数名 去空格或或其他字 符: trim() 描述 实例 输入
删除字符串两端的空格 或其他预定义字符 删除字符串右边的空格 或其他预定义字符 rtrim()的别名 删除字符串左边的空格 或其他预定义字符 返回路径中的目录部分
$str = "\r\nHello World!\r\n"; echo trim($str);
str_repeat() str_split() strrev() wordwrap() str_shuffle()
要填充的字符串|新字 符串的长度|供填充使 用的字符串,默认是空 白 要重复的字符串|字符 串将被重复的次数 要分割的字符串|每个 数组元素的长度,默认 1 目标字符串
$str = "An example on a long word 目标字符串|最大宽数 is: Supercalifragulistic"; echo wordwrap($str,15); 目标字符串
两个标字符串
目标字符串
将字符串分成小块 切开字符串 使用一个字符串为标志 分割另一个字符串 同join,将数组值用预 订字符连接成字符串 截取字符串
str chunk_split(str $body[,int $len[,str $end]]) str strtok(str $str,str $token) array explode(str $sep,str $str[,int $limit]) string implode ( string $glue , array $pieces ) string substr ( string $string , int $start [, int $length ] )
$body目标字串,$len长 度,$str插入结束符 目标字符串$str,以 $token为标志切割 $sep为分割符,$str目 标字符串,$limit返回 数组最多包含元素数 $glue默认,用''则直 接相连
$search查找的字符 字符串替换操作,区分 mix str_replace(mix $search,,mix 串,$replace替换的字 大小写 $replace,mix $subject[,int &$num]) 符串,$subject被查找 字串,&$num
目标字符串|指定的特 定字符或字符范围
stripcslashes() addslashes() stripslashes()
目标字符串
quotemeta()
chr() ord() 字符串比较: strcasecmp() strcmp() strncmp() strncasecmp() strnatcmp() strnatcasecmp() 字符串切割与拼接: chunk_split() strtok() explode() implode() substr() 字符串查找替换: str_replace()
统计字符串含有的单词 mix str_word_count(str $str,[]) 数 统计字符串长度 int strlen(str $str) 统计字符串中所有字母 mixed count_chars ( string $string 出现次数(0..255) [, int $mode ] )
addcslashes()
echo nl2br("One line.\nAnother line."); echo strip_tags("Hello <b>world!</b>"); $str = "Hello, my name is John Adams."; echo $str; echo addcslashes($str,'m'); echo stripcslashes("Hello, \my na\me is Kai Ji\m."); $str = "Who's John Adams?";echo addslashes($str); echo stripslashes("Who\'s John Adams?"); $str = "Hello world. (can you hear me?)"; echo quotemeta($str); echo chr(052); echo ord("hello");
两个比较的字符串
strcspn() 字符串统计: str_word_count() strlen() count_chars()
$str1被查询,$str2查 int strcspn ( string $str1 , 询字符串,$start开始 string $str2 [, int $start [, int 查询的字符,$length $length ]] ) 查询长度 目标字符串 目标字符串
str_ireplace()
$search查找的字符 mix str_ireplace ( mix $search , 字符串替换操作,不区 串,$replace替换的字 mix $replace , mix $subject [, int 分大小写 符串,$subject被查找 &$count ] ) 字串,&$num int substr_count ( string 统计一个字符串,在另 $haystack , string $needle [, int 一个字符串中出现次数 $offset = 0 [, int $length ]] ) mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] ) int similar_text(str $str1,str $str2)
substr_count()
substr_replace() similar_text()
替换字符串中某串为另 一个字符串
strrchr()
strstr()
strchr()
stristr()
strtr() strpos()
stripos()
strrpos()
strripos()
strspn()
返回两字符串相同字符 的数量 返回一个字符串在另一 个字符串中最后一次出 string strrchr ( string $haystack , mixed $needle ) 现位置开始到末尾的字 符串 返回一个字符串在另一 string strstr ( string $str, 个字符串中开始位置到 string $needle , bool 结束的字符串 $before_needle ) strstr()的别名,返回 string strstr ( string $haystack , 一个字符串在另一个字 mixed $needle [, bool 符串中首次出现的位置 $before_needle = false ] ) 开始到末尾的字符串 返回一个字符串在另一 string stristr ( string $haystack 个字符串中开始位置到 , mixed $needle [, bool 结束的字符串,不区分 $before_needle = false ] ) 大小写 转换字符串中的某些字 string strtr ( string $str , 符 string $from , string $to ) int strpos ( string $haystack , 寻找字符串中某字符最 mixed $needle [, int $offset = 0 ] 先出现的位置 ) 寻找字符串中某字符最 int stripos ( string $haystack , 先出现的位置,不区分 string $needle [, int $offset ] ) 大小写 int strrpos ( string $haystack , 寻找某字符串中某字符 string $needle [, int $offset = 0 最后出现的位置 ] ) 寻找某字符串中某字符 int strripos ( string $haystack , 最后出现的位置,不区 string $needle [, int $offset ] ) 分大小写 int strspn ( string $str1 , string 返回字符串中首次符合 $str2 [, int $start [, int $length mask的子字符串长度 ]] ) 返回字符串中不符合 mask的字符串的长度
ASCII 值 字符串
不区分大小写比较两字 echo strcasecmp("Hello 符串 world!","HELLO WORLD!"); 区分大小写比较两字符 串 比较字符串前n个字符, int strncmp ( string $str1 , 区分大小写 string $str2 , int $len ) 比较字符串前n个字符, int strncasecmp ( string $str1 , 不区分大小写 string $str2 , int $len ) 自然顺序法比较字符串 int strnatcmp ( string $str1 , 长度,区分大小写 string $str2 ) 自然顺序法比较字符串 int strnatcasecmp ( string $str1 , 长度,不区分大小写 string $str2 )