substr_count

(PHP 4, PHP 5)

substr_count副文字列の出現回数を数える

説明

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

substr_count() は、文字列 haystack の中での副文字列 needle の出現回数を返します。 needle は英大小文字を区別することに注意してください。

注意:

この関数は重なり合う副文字列をカウントしません。以下の例を見てください !

パラメータ

haystack

検索対象の文字列

needle

検索する副文字列

offset

開始位置のオフセット

length

指定したオフセット以降に副文字列で検索する最大長。 オフセットと長さの総和が haystack の長さよりも長い場合、警告が発生します。

返り値

この関数は 整数 を返します。

変更履歴

バージョン 説明
5.1.0 offsetlength パラメータが追加されました。

例1 substr_count() の例

<?php
$text 
'This is a test';
echo 
strlen($text); // 14

echo substr_count($text'is'); // 2

// 文字列は 's is a test' になっているので, 1 が表示される
echo substr_count($text'is'3);

// テキストは 's i' になっているので, 0 が表示される
echo substr_count($text'is'33);

// 5+10 > 14 なので、警告が発生する
echo substr_count($text'is'510);


// 重なっている副文字列はカウントされないので、1 が表示される
$text2 'gcdgcdgcd';
echo 
substr_count($text2'gcdgcd');
?>

参考

  • count_chars() - 文字列で使用されている文字に関する情報を返す
  • strpos() - 文字列内の部分文字列が最初に現れる場所を見つける
  • substr() - 文字列の一部分を返す
  • strstr() - 文字列が最初に現れる位置を見つける

add a note add a note

User Contributed Notes 7 notes

up
7
jrhodes at roket-enterprises dot com
4 years ago
It was suggested to use

substr_count ( implode( $haystackArray ), $needle );

instead of the function described previously, however this has one flaw.  For example this array:

array (
  0 => "mystringth",
  1 => "atislong"
);

If you are counting "that", the implode version will return 1, but the function previously described will return 0.
up
3
gigi at phpmycoder dot com
5 years ago
below was suggested a function for substr_count'ing an array, yet for a simpler procedure, use the following:

<?php
substr_count
( implode( $haystackArray ), $needle );
?>
up
3
info at fat-fish dot co dot il
6 years ago
a simple version for an array needle (multiply sub-strings):
<?php

function substr_count_array( $haystack, $needle ) {
    
$count = 0;
     foreach (
$needle as $substring) {
         
$count += substr_count( $haystack, $substring);
     }
     return
$count;
}
?>
up
1
flobi at flobi dot com
7 years ago
Making this case insensitive is easy for anyone who needs this.  Simply convert the haystack and the needle to the same case (upper or lower).

substr_count(strtoupper($haystack), strtoupper($needle))
up
2
XinfoX X at X XkarlX X-X XphilippX X dot X XdeX
10 years ago
Yet another reference to the "cgcgcgcgcgcgc" example posted by "chris at pecoraro dot net":

Your request can be fulfilled with the Perl compatible regular expressions and their lookahead and lookbehind features.

The example

 $number_of_full_pattern = preg_match_all('/(cgc)/', "cgcgcgcgcgcgcg", $chunks);

works like the substr_count function. The variable $number_of_full_pattern has the value 3, because the default behavior of Perl compatible regular expressions is to consume the characters of the string subject that were matched by the (sub)pattern. That is, the pointer will be moved to the end of the matched substring.
But we can use the lookahead feature that disables the moving of the pointer:

 $number_of_full_pattern = preg_match_all('/(cg(?=c))/', "cgcgcgcgcgcgcg", $chunks);

In this case the variable $number_of_full_pattern has the value 6.
Firstly a string "cg" will be matched and the pointer will be moved to the end of this string. Then the regular expression looks ahead whether a 'c' can be matched. Despite of the occurence of the character 'c' the pointer is not moved.
up
0
qeremy [atta] gmail [dotta] com
6 months ago
Unicode example with "case-sensitive" option;

<?php
function substr_count_unicode($str, $substr, $caseSensitive = true, $offset = 0, $length = null) {
    if (
$offset) {
       
$str = substr_unicode($str, $offset, $length);
    }

   
$pattern = $caseSensitive
       
? '~(?:'. preg_quote($substr) .')~u'
       
: '~(?:'. preg_quote($substr) .')~ui';
   
preg_match_all($pattern, $str, $matches);

    return isset(
$matches[0]) ? count($matches[0]) : 0;
}

function
substr_unicode($str, $start, $length = null) {
    return
join('', array_slice(
       
preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY), $start, $length));
}

$s = 'Ümit yüzüm gözüm...';
print
substr_count_unicode($s, 'ü');            // 3
print substr_count_unicode($s, 'ü', false);     // 4
print substr_count_unicode($s, 'ü', false, 10); // 1

print substr_count_unicode($s, 'üm');           // 2
print substr_count_unicode($s, 'üm', false);    // 3
?>
up
-4
chrisstocktonaz at gmail dot com
4 years ago
In regards to anyone thinking of using code contributed by zmindster at gmail dot com

Please take careful consideration of possible edge cases with that regex, in example:

$url = 'http://w3.host.tld/path/to/file/..../file.extension';
$url = 'http://w3.host.tld/path/to/file/../file.extension?malicous=....';

This would cause a infinite loop and for example be a possible entry point for a denial of service attack. A correct fix would require additional code, a quick hack would be just adding a additional check, without clarity or performance in mind:

...
$i = 0;
while (substr_count($url, '../') && ++$i < strlen($url))
...

-Chris
To Top