PHP 8.4.0 RC4 available for testing

substr_count

(PHP 4, PHP 5, PHP 7, PHP 8)

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

説明

substr_count(
    string $haystack,
    string $needle,
    int $offset = 0,
    ?int $length = null
): int

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

注意:

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

パラメータ

haystack

検索対象の文字列

needle

検索する副文字列

offset

開始位置のオフセット。 負の数を指定すると、文字列の末尾から数えます。

length

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

戻り値

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

変更履歴

バージョン 説明
8.0.0 length は、nullable になりました。
7.1.0 負の offsetlength をサポートするようになりました。 length は、0 を指定しても問題ありません。

例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', 3, 3);

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


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

参考

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

add a note

User Contributed Notes 7 notes

up
58
tuxedobob
8 years ago
It's worth noting this function is surprisingly fast. I first ran it against a ~500KB string on our web server. It found 6 occurrences of the needle I was looking for in 0.0000 seconds. Yes, it ran faster than microtime() could measure.

Looking to give it a challenge, I then ran it on a Mac laptop from 2010 against a 120.5MB string. For one test needle, it found 2385 occurrences in 0.0266 seconds. Another test needs found 290 occurrences in 0.114 seconds.

Long story short, if you're wondering whether this function is slowing down your script, the answer is probably not.
up
12
flobi at flobi dot com
18 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
3
tweston at bangordailynews dot com
9 years ago
To account for the case that jrhodes has pointed out, we can change the line to:

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

This way:

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

Becomes

mystringth,atislong

Which brings the count for $needle = "that" to 0 again.
up
-1
jrhodes at roket-enterprises dot com
15 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
-1
info at fat-fish dot co dot il
17 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
XinfoX X at X XkarlX X-X XphilippX X dot X XdeX
20 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
-5
php at blink dot at
10 years ago
This will handle a string where it is unknown if comma or period are used as thousand or decimal separator. Only exception where this leads to a conflict is when there is only a single comma or period and 3 possible decimals (123.456 or 123,456). An optional parameter is passed to handle this case (assume thousands, assume decimal, decimal when period, decimal when comma). It assumes an input string in any of the formats listed below.

function toFloat($pString, $seperatorOnConflict="f")
{
$decSeperator=".";
$thSeperator="";

$pString=str_replace(" ", $thSeperator, $pString);

$firstPeriod=strpos($pString, ".");
$firstComma=strpos($pString, ",");
if($firstPeriod!==FALSE && $firstComma!==FALSE) {
if($firstPeriod<$firstComma) {
$pString=str_replace(".", $thSeperator, $pString);
$pString=str_replace(",", $decSeperator, $pString);
}
else {
$pString=str_replace(",", $thSeperator, $pString);
}
}
else if($firstPeriod!==FALSE || $firstComma!==FALSE) {
$seperator=$firstPeriod!==FALSE?".":",";
if(substr_count($pString, $seperator)==1) {
$lastPeriodOrComma=strpos($pString, $seperator);
if($lastPeriodOrComma==(strlen($pString)-4) && ($seperatorOnConflict!=$seperator && $seperatorOnConflict!="f")) {
$pString=str_replace($seperator, $thSeperator, $pString);
}
else {
$pString=str_replace($seperator, $decSeperator, $pString);
}
}
else {
$pString=str_replace($seperator, $thSeperator, $pString);
}
}
return(float)$pString;
}

function testFloatParsing() {
$floatvals = array(
"22 000",
"22,000",
"22.000",
"123 456",
"123,456",
"123.456",
"22 000,76",
"22.000,76",
"22,000.76",
"22000.76",
"22000,76",
"1.022.000,76",
"1,022,000.76",
"1,000,000",
"1.000.000",
"1022000.76",
"1022000,76",
"1022000",
"0.76",
"0,76",
"0.00",
"0,00",
"1.00",
"1,00",
"-22 000,76",
"-22.000,76",
"-22,000.76",
"-22 000",
"-22,000",
"-22.000",
"-22000.76",
"-22000,76",
"-1.022.000,76",
"-1,022,000.76",
"-1,000,000",
"-1.000.000",
"-1022000.76",
"-1022000,76",
"-1022000",
"-0.76",
"-0,76",
"-0.00",
"-0,00",
"-1.00",
"-1,00"
);

echo "<table>
<tr>
<th>String</th>
<th>thousands</th>
<th>fraction</th>
<th>dec. if period</th>
<th>dec. if comma</th>
</tr>";

foreach ($floatvals as $fval) {
echo "<tr>";
echo "<td>" . (string) $fval . "</td>";

echo "<td>" . (float) toFloat($fval, "") . "</td>";
echo "<td>" . (float) toFloat($fval, "f") . "</td>";
echo "<td>" . (float) toFloat($fval, ".") . "</td>";
echo "<td>" . (float) toFloat($fval, ",") . "</td>";
echo "</tr>";
}
echo "</table>";
}
To Top