天天看点

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

  • strncmp

    示例:

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

应用实例:

类似函数:substr_compare

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

Returns

FALSE

if the needle was not found.

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

\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

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

  • explode
Split a string by string

注意事项:

(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(’’, “”)将返回,

[""]

, 及包含一个空字符串的数组.