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(’’, “”)将返回, [""], 及包含一个空字符串的数组.

你可能感兴趣的:(php,PHP常用字符串函数及注意事项,PHP常用字符串函数)