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
    \r\0\x0B"
    ] )

    t水平タブ、キーボードTabキーに対応する.linux改行記号、r macOs改行記号(アップルのオペレーティングシステム)、r windows改行記号0はc言語では文字列の末尾フラグとして使用するが、phpでは特に意味がない.x 0 Bは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
    \f\v"
    ] )

    substrこの関数の3番目のパラメータに注意
  • explode

  • Split a string by string
    array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )  
    

    注意:(1)第1のパラメータは区切り文字であり、第2のパラメータは区切り文字列である.3番目のパラメータは被スタイルの総個数である.戻り値の重要な説明:
    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(’’,"")は、[""]、および空の文字列を含む配列を返す.