ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • substr_count() 함수
    개발 관련/PHP 2011. 5. 12. 11:41


    int substr_count ( string $haystack , string $needle [, int $offset= 0 [, int $length ]] )

    Count the number of substring occurrences
    해당 글에(string $haystack)에 string $needle 글자가 몇번 있는지 세는 함수

    [
      int $offset -> 몇번째 글 부터 찾을 건지
      int $length  -> 몇번째 글 까지 찾을 건지
    ]

    $text = 'This is a test';
    echo strlen($text);  // 14
    echo substr_count($text, 'is'); // 2
    
    // the string is reduced to 's is a test', so it prints 1
    echo substr_count($text, 'is', 3);
    
    // the text is reduced to 's i', so it prints 0
    echo substr_count($text, 'is', 3, 3);
    
    // generates a warning because 5+10 > 14
    echo substr_count($text, 'is', 5, 10);
    
    // prints only 1, because it doesn't count overlapped subtrings
    $text2 = 'gcdgcdgcd';
    echo substr_count($text2, 'gcdgcd');
    


    결과값:
    14

    2

    1

    0


    Warning: substr_count() [
    function.substr-count]: Length value 10 exceeds string length. in -------------.php on line 17


    1


    참고: http://kr.php.net/manual/kr/function.substr-count.php

     

     

     

    '개발 관련 > PHP' 카테고리의 다른 글

    urlencode(), urldecode() 함수  (0) 2011.03.24
    join()  (0) 2011.03.23
    htmlentities  (0) 2011.03.22
    Date() 함수  (0) 2011.03.17
    chr  (0) 2011.03.14
Designed by Tistory.