ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ch4. 문자열 조작과 정규표현
    플밍/PHP+MySQL 2011. 8. 29. 05:26
    ch4. 문자열 조작과 정규표현

    2007/04/20 02:33


    (그냥 PHP api 문서 참조하자 ㅡㅡ;; http://kr2.php.net/manual/en/index.php)

     

    - 문자열 정돈을 위한 함수

     trim(string) 앞뒤 공백 제거(\n, \r, \t, \v, \0)

     ltrim(string) 앞 공백 제거

     chop(string) 뒤 공백 제거

     

    - 포맷팅

     nl2br(string) string에 포함된 개행문자를 <br>로 자동으로 바꿔준다.

           폼에서 입력받은것을 다시 내보낼때 유용.

     printf() C에서의 것과 동일(%d, %c, %s, %x.xf ...)

     * 대소문자 변환

     strtoupper(string) string을 대문자로

     strtolower(string) string을 소문자로

     ucfirst(string) 맨 앞 글자만 대문자로

     ucwords(string) 각 단어의 첫글자를 대문자로

     

    - MySQL에 저장할때 필요한 함수

     DB에 따라 특수문자(인용부호, 역슬래쉬, 널문자 등..)를 제어기호로 받아들이는 경우가

     있다. 그래서 이런 특수문자를 문자 그대로 인식 시키기 위해선...

     AddSlaches(string) 특수문자 앞에 \를 넣어준다.  (DB에 저장할때)

     StripSlashes(string) 특수문자 앞에 붙은 \를 제거한다. (DB에서 가져올때)

     

    - 문자열 분리, 합체

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

        Split a string by string

     string implode ( string $glue, array $pieces ) = join(...)
        Join array elements with a string

     string strtok ( string $str, string $token )

        Tokenize string

     string substr ( string $string, int $start [, int $length] )

        Return part of a string

     

    - 문자열 비교

     int strcmp ( string $str1, string $str2 ) case sensitive

        Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2,

        and 0 if they are equal.

     int strcasecmp ( string $str1, string $str2 ) = strcmp() case insensitive

     int strnatcmp ( string $str1, string $str2 )
        String comparisons using a "natural order" algorithm

     int strlen ( string $string )

        Returns the length of the given string.

     

    - 문자열 찾기, 대체하기

     string strstr ( string $haystack, string $needle ) === strchr() case sensitive

        Returns part of haystack string from the first occurrence of needle to the end of     

        haystack.

     string stristr ( string $haystack, string $needle ) = strstr() case insensitive
     string strrchr ( string $haystack, string $needle )

        This function returns the portion of haystack which starts at the last occurrence

        of needle and goes until the end of haystack.

     int strpos ( string $haystack, mixed $needle [, int $offset] )

        Returns the numeric position of the first occurrence of needle in the haystack

        string. Unlike the strrpos() before PHP 5, this function can take a full string as   

        the needle parameter and the entire string will be used.

     int strrpos ( string $haystack, string $needle [, int $offset] )

        Returns the numeric position of the last occurrence of needle in the haystack

        string. Note that the needle in this case can only be a single character in PHP 4.

        If a string is passed as the needle, then only the first character of that string will

        be used.

    ------------------------------------------------------------------------------------

    !!! It is easy to mistake the return values for "character found at position 0" and

      "character not found". Here's how to detect the difference:


     
    ------------------------------------------------------------------------------------

     

     mixed str_replace ( mixed $search, mixed $replace, mixed $subject [, int &$count] )

        This function returns a string or an array with all occurrences of search in

        subject replaced with the given replace value.

     
     mixed substr_replace ( mixed $string, string $replacement, int $start [, int $length] )

        Replaces a copy of string delimited by the start and (optionally) length  
     
        parameters with the string given in replacement(With this function, you can also
     
        insert or delete string fragments.)

     

    '플밍 > PHP+MySQL' 카테고리의 다른 글

    # Comparison Operators  (0) 2011.08.29
    # Regular Expressions  (0) 2011.08.29
    ch3. 배열 사용  (0) 2011.08.29
    ch2. 데이터 저장과 불러오기  (0) 2011.08.29
    ch1. PHP집중훈련  (0) 2011.08.29
Designed by Tistory.