天天看點

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

[""]

, 及包含一個空字元串的數組.