关于PHP常用字符串总结

这是一些常用的PHP字符串

函数名 描述 实例

  • trim() 删除字符串两端的空格或其他预定义字符 “KaTeX parse error: Can't use function '\r' in math mode at position 8: str = "\̲r̲\nHello World!\…str); rtrim() 删除字符串右边的空格或其他预定义字符 “KaTeX parse error: Undefined control sequence: \n at position 20: …= "Hello World!\̲n̲\n"; echo rtrim…str);” chop() rtrim()的别名 同上
    ltrim() 删除字符串左边的空格或其他预定义字符 “KaTeX parse error: Can't use function '\r' in math mode at position 8: str = "\̲r̲\nHello World!"…str);” dirname() 回路径中的目录部分(我们把它归在了字符串函数里了) echo
    dirname(“c:/testweb/home.php”); str_pad() 把字符串填充为指定的长度 s t r = " H e l l o W o r l d " ; e c h o s t r p a d ( str = "Hello World"; echo str_pad( str="HelloWorld";echostrpad(str,20,”."); str_repeat() 重复使用指定字符串 echo
    str_repeat(".",13);
    str_split() 把字符串分割到数组中 print_r(str_split(“Hello”));
    strrev() 反转字符串 echo strrev(“Hello World!”);
    wordwrap() 按照指定长度对字符串进行折行处理 “ s t r = " " A n e x a m p l e o n a l o n g w o r d i s : S u p e r c a l i f r a g u l i s t i c " " ; e c h o w o r d w r a p ( str = ""An example on a long word is: Supercalifragulistic""; echo wordwrap( str=""Anexampleonalongwordis:Supercalifragulistic"";echowordwrap(str,15);”
    str_shuffle() 随机地打乱字符串中所有字符 echo str_shuffle(“Hello World”);
    parse_str() 将字符串解析成变量 “parse_str(“id=23&name=John%20Adams”, m y A r r a y ) ; p r i n t r ( myArray); print_r( myArray);printr(myArray);” number_format() 通过千位分组来格式化数字 “echo
    number_format(“1000000”); echo number_format(“1000000”,2); echo
    number_format(“1000000”,2,”","",""."");" strtolower() 字符串转为小写 echo
    strtolower(“Hello WORLD!”); strtoupper() 字符串转为大写 echo
    strtoupper(“Hello WORLD!”); ucfirst() 字符串首字母大写 echo ucfirst(“hello
    world”); ucwords() 字符串每个单词首字符转为大写 echo ucwords(“hello world”);
    htmlentities() 把字符转为HTML实体 KaTeX parse error: Expected 'EOF', got '&' at position 14: str = ""John &̲ 'Adams'""; ech…str, ENT_COMPAT); htmlspecialchars() 预定义字符转html编码
    nl2br() \n转义为 标签 echo nl2br(“One line.\nAnother line.”);
    strip_tags() 剥去 HTML、XML 以及 PHP 的标签 echo strip_tags(“Hello world!”);
    addcslashes() 在指定的字符前添加反斜线转义字符串中字符 $str = ““Hello, my name is John
    Adams.” echo s t r ; e c h o a d d c s l a s h e s ( str; echo addcslashes( str;echoaddcslashes(str,‘m’);”
    stripcslashes() 删除由addcslashes()添加的反斜线 echo stripcslashes(“Hello, \my
    na\me is Kai Ji\m.”); addslashes() 指定预定义字符前添加反斜线 s t r = " W h o ′ s J o h n A d a m s ? " ; e c h o a d d s l a s h e s ( str = "Who's John Adams?";echo addslashes( str="WhosJohnAdams?";echoaddslashes(str);
    stripslashes() 删除由addslashes()添加的转义字符 echo stripslashes(“Who’s John
    Adams?”); quotemeta() 在字符串中某些预定义的字符前添加反斜线 s t r = " H e l l o w o r l d . ( c a n y o u h e a r m e ? ) " ; e c h o q u o t e m e t a ( str = "Hello world. (can you hear me?)"; echo quotemeta( str="Helloworld.(canyouhearme?)";echoquotemeta(str); chr() 从指定的 ASCII 值返回字符 echo
    chr(052); ord() 返回字符串第一个字符的 ASCII值 echo ord(“hello”);
    strcasecmp() 不区分大小写比较两字符串 echo strcasecmp(“Hello world!”,“HELLO
    WORLD!”); strcmp() 区分大小写比较两字符串 strncmp() 比较字符串前n个字符,区分大小写
    strncasecmp() 比较字符串前n个字符,不区分大小写 int strncasecmp ( string $str1 ,
    string $str2 , int $len ) strnatcmp() 自然顺序法比较字符串长度,区分大小写 int
    strnatcmp ( string $str1 , string $str2 )
    strnatcasecmp() 自然顺序法比较字符串长度,不区分大小写 int strnatcasecmp ( string $str1
    , string $str2 ) chunk_split() 将字符串分成小块 str chunk_split(str
    $body[,int $len[,str $end]]) strtok() 切开字符串 str strtok(str $str,str
    $token) explode() 使用一个字符串为标志分割另一个字符串 array explode(str $sep,str
    $str[,int $limit]) implode() 同join,将数组值用预订字符连接成字符串 string implode (
    string $glue , array $pieces ) substr() 截取字符串 string substr ( string
    $string , int $start [, int $length ] )
    str_replace() 字符串替换操作,区分大小写 mix str_replace(mix $search,mix
    $replace,mix KaTeX parse error: Expected 'EOF', got '&' at position 14: subject[,int &̲num]) str_ireplace() 字符串替换操作,不区分大小写 mix
    str_ireplace ( mix $search , mix $replace , mix KaTeX parse error: Expected 'EOF', got '&' at position 16: subject [, int &̲count ] ) substr_count() 统计一个字符串,在另一个字符串中出现次数 int substr_count (
    string $haystack , string $needle [, int $offset = 0 [, int $length
    ]] ) substr_replace() 替换字符串中某串为另一个字符串 mixed substr_replace ( mixed
    $string , string $replacement , int $start [, int $length ] )
    similar_text() 返回两字符串相同字符的数量 int similar_text(str $str1,str $str2)
    strchr() 返回一个字符串在另一个字符串中开始位置到结束的字符串 string strstr ( string $str,
    string $needle , bool $before_needle )
    strrchr() 返回一个字符串在另一个字符串中最后一次出现位置开始到末尾的字符串 string strrchr ( string
    $haystack , mixed $needle )
    stristr() 返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写 string stristr ( string
    $haystack , mixed $needle [, bool $before_needle = false ] )
    strtr() 转换字符串中的某些字符 string strtr ( string $str , string $from ,
    string $to ) strpos() 寻找字符串中某字符最先出现的位置 int strpos ( string $haystack
    , mixed $needle [, int $offset = 0 ] )
    stripos() 寻找字符串中某字符最先出现的位置,不区分大小写 int stripos ( string $haystack ,
    string $needle [, int $offset ] ) strrpos() 寻找某字符串中某字符最后出现的位置 int
    strrpos ( string $haystack , string $needle [, int $offset = 0 ] )
    strripos() 寻找某字符串中某字符最后出现的位置,不区分大小写 int strripos ( string $haystack ,
    string $needle [, int $offset ] ) strspn() 返回字符串中首次符合mask的子字符串长度 int
    strspn ( string $str1 , string $str2 [, int $start [, int $length ]]
    ) strcspn() 返回字符串中不符合mask的字符串的长度 int strcspn ( string $str1 , string
    $str2 [, int $start [, int $length ]] )
    str_word_count() 统计字符串含有的单词数 mix str_word_count(str $str,[])
    strlen() 统计字符串长度 int strlen(str $str)
    count_chars() 统计字符串中所有字母出现次数(0…255) mixed count_chars ( string
    $string [, int $mode ] ) md5() 字符串md5编码 s t r = " H e l l o " ; e c h o m d 5 ( str = "Hello"; echo md5( str="Hello";echomd5(str) iconv mb_substr 获取字符串的部分 string mb_substr ( string $str ,
    int $start [, int $length = NULL [, string $encoding =
    mb_internal_encoding() ]] ) mb_http_output 设置/获取 HTTP 输出字符编码 mixed
    mb_http_output ([ string $encoding = mb_http_output() ] )
    mb_strlen 获取字符串的长度 mixed mb_strlen ( string $str [, string $encoding
    = mb_internal_encoding() ] ) iconv 字符串按要求的字符编码来转换 string iconv ( string $in_charset , string $out_charset , string $str )
    iconv_substr 截取字符串的部分
    iconv_get_encoding 获取 iconv 扩展的内部配置变量
    mb_substr_count 统计字符串出现的次数
    mb_check_encoding 检查字符串在指定的编码里是否有效
    mb_strrpos 查找字符串在一个字符串中最后出现的位置
    mb_split 使用正则表达式分割多字节字符串
    parse_url 解释URL成为一个数组
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值