Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

substr_count

Description

The substr_count of String for PHP count the number of substring occurrences.

Syntax

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

Parameters

haystack

The string to search in

needle

The substring to search for

offset

The offset where to start counting. If the offset is negative, counting starts from the end of the string.

length

The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the haystack length. A negative length counts from the end of haystack.

Return

Returns an int.

Examples

1 · haystack needle

<?

$haystack = "abcdeabcdeabcde";
$needle = "abcde";

$return = substr_count($haystack, $needle);

echo $return;

?>
3

2 · offset · negative

<?

$haystack = "abcdeabcdeabcde";
$needle = "abcde";
$offset = -5;

$return = substr_count($haystack, $needle, $offset);

echo $return;

?>
1

3 · offset · non-negative

<?

$haystack = "abcdeabcdeabcde";
$needle = "abcde";
$offset = 5;

$return = substr_count($haystack, $needle, $offset);

echo $return;

?>
2

4 · length · negative

<?

$haystack = "abcdeabcdeabcde";
$needle = "abcde";
$offset = 5;
$length = -5;

$return = substr_count($haystack, $needle, $offset, $length);

echo $return;

?>
1

5 · length · non-negative

<?

$haystack = "abcdeabcdeabcde";
$needle = "abcde";
$offset = 5;
$length = 5;

$return = substr_count($haystack, $needle, $offset, $length);

echo $return;

?>
1
HomeMenu