PHP常用字符串函数及注意事项

  • strncmp
    示例:
strncmp($alias, '@', 1)
  • strcasecmp

不区分大小写,比较结果相同的情况下,会返回0

应用实例:

$result[$matches[1]] = strcasecmp($matches[2], 'desc') ? SORT_ASC : SORT_DESC;  

类似函数:substr_compare

  • strpos

Find the position of the first occurrence of a substring in a string

Returns FALSE if the needle was not found.

false|int strpos ($haystack, $needle, $offset = 0)
  • trim

Strip whitespace (or other characters) from the beginning and end of a string

string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )  

\t 水平制表符,对应键盘 Tab 键.
\n linux换行符, \r macOs 换行符(苹果操作系统), \n\r windows换行符
\0 在c语言中,用作字符串结尾标志,而在php中,无特殊含义.
\x0B 等同于\v,垂直指标符号, 注意最后一个字母为B,不要写成8了.

类似函数: ltrim rtrim

  • strtr

Translate characters or replace substrings

string strtr ( string $str , string $from , string $to ) 
string strtr ( string $str , array $replace_pairs )  
  • str_replace

Replace all occurrences of the search string with the replacement string

mixed str_replace ( mixed $needle , mixed $replace , mixed $haystack [, int &$count ] )  
  • ucwords

Uppercase the first character of each word in a string

string ucwords ( string $str [, string $delimiters = " \t\r\n\f\v" ] )  

substr 注意这个函数的第三个参数

  • explode

Split a string by string

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )  

注意事项:
(1) 第一个参数为分隔符,第二个参数是被分隔的字符串.第三个参数为被风格的总个数.
返回值重要说明:

If delimiter is an empty string (""), explode() will return FALSE.
A string that doesn’t contain the delimiter will simply return a one-length array of the original string.
If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

explode(’’, “”)将返回, [""], 及包含一个空字符串的数组.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值