Forum PHP 2017


(PHP 4, PHP 5, PHP 7)

substr_count Ermittelt, wie oft eine Zeichenkette in einem String vorkommt


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

Die Funktion substr_count() ermittelt, wie oft needle in dem String haystack vorkommt, und gibt die Anzahl der Vorkommen zurück. Beachten Sie, dass der Parameter needle case sensitive ist.


Die Funktion zählt einander überlappende Substrings nicht mit. Beachten Sie das untenstehende Beispiel!



Der String, in dem gesucht werden soll


Der Substring, nach dem gesucht werden soll


Die Zeichenposition, an der die Zählung begonnen werden soll


Die maximale Länge nach dem angegebenen Offset, in der nach dem Substring gesucht werden soll. Es wird eine Warnung ausgegeben, wenn Offset plus Länge größer als die Länge von haystack sind.


Die Funktion gibt einen Wert vom Typ integer zurück.


Version Beschreibung
5.1.0 Hinzufügen der Parameter offset und length


Beispiel #1 Ein substr_count() Beispiel

'Dies ist ein Test';
strlen($text); // 17

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

// wird der String auf 's ist ein Test' reduziert,
// lautet das ausgegebene Ergebnis 1
echo substr_count($text'es'3);

// wird der String auf 's i' reduziert,
// lautet das Ergebnis 0
echo substr_count($text'es'33);

// generiert eine Warnung, da 5+13 > 17
echo substr_count($text'es'513);

// gibt 1 aus, da überlappende Substrings nicht gezählt werden
$text2 'gcdgcdgcd';

Siehe auch

  • count_chars() - Gibt Informationen über die in einem String enthaltenen Zeichen zurück
  • strpos() - Sucht das erste Vorkommen des Suchstrings
  • substr() - Gibt einen Teil eines Strings zurück
  • strstr() - Findet das erste Vorkommen eines Strings

add a note add a note

User Contributed Notes 10 notes

1 year 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.
jrhodes at roket-enterprises dot com
8 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.
flobi at flobi dot com
10 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))
tweston at bangordailynews dot com
2 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"



Which brings the count for $needle = "that" to 0 again.
php at blink dot at
2 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")

    $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) {
        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);

function testFloatParsing() {
    $floatvals = array(
        "22 000",
        "123 456",
        "22 000,76",
        "-22 000,76",
        "-22 000",
    echo "<table>
            <th>dec. if period</th>
            <th>dec. if comma</th>
    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>";
gigi at phpmycoder dot com
8 years ago
below was suggested a function for substr_count'ing an array, yet for a simpler procedure, use the following:

( implode( $haystackArray ), $needle );
info at fat-fish dot co dot il
10 years ago
a simple version for an array needle (multiply sub-strings):

function substr_count_array( $haystack, $needle ) {
$count = 0;
     foreach (
$needle as $substring) {
$count += substr_count( $haystack, $substring);
XinfoX X at X XkarlX X-X XphilippX X dot X XdeX
13 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.
qeremy [atta] gmail [dotta] com
3 years ago
Unicode example with "case-sensitive" option;

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;

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

$s = 'Ümit yüzüm gözüm...';
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
chrisstocktonaz at gmail dot com
7 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 = '';
$url = '';

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))

To Top