substr_count
COUNT the number of substring occurrences.
If $offset < 0 the counting starts from the end of the STRING.
This function outputs a WARNING if the $offset plus the $length is greater than the $haystack length.
A negative $length counts from the end of $haystack.
<?php
int substr_count ( str $haystack ,
str $needle [,
int $offset = 0 [,
int $length ]] )
where,
$haystack = The input STRING
$needle = The needle to be found
$offset = The offset where to start counting
$length = The maximum length after the
specified offset to search for the substring
?>
$haystack
The STRING to search in.
$needle
The substring to search for.
$offset
The $offset where to start counting.
$length
The maximum $length after the specified $offset to search for the substring.
EXERCISE
<?php
$str01 = 'A man is known by the company he keeps.';
$len_str01 = strlen($str01);
$needle01 = 'he';
$offset01 = 0;
$length01 = $len_str01;
$countneedle01 = substr_count($str01,
$needle01,
$offset01,
$length01);
echo "There are $countneedle01
occurrences of '$needle01' on STRING:<br>
$str01<br>";
echo '<br>Offset = ' . $offset01 .
'<br>Length = ' . $length01 . '<br>';
?>
EXERCISE
<?php
$str02 = 'A man is known by the company he keeps.';
$len_str02 = strlen($str02);
$needle02 = 'he';
$offset02 = 20;
$length02 = $len_str02 - 20;
$countneedle02 = substr_count($str02,
$needle02,
$offset02,
$length02);
echo "There are $countneedle02
occurrences of '$needle02' on STRING:<br>
$str02<br>";
echo '<br>Offset = ' . $offset02 .
'<br>Length = ' . $length02 . '<br>';
?>
EXERCISE
<?php
$str03 = 'A man is known by the company he keeps.';
$len_str03 = strlen($str03);
$needle03 = 'he';
$offset03 = mt_rand(-$len_str03, $len_str03);
$length03 = mt_rand(-$len_str03, $len_str03);
$countneedle03 = @substr_count($str03,
$needle03,
$offset03,
$length03);
echo "There are $countneedle03
occurrences of '$needle03' on STRING:<br>
$str03<br>";
echo '<br>Offset = ' . $offset03 .
'<br>Length = ' . $length03 . '<br>';
?>
EXERCISE
<?php
$str04 = 'A man is known by the company he keeps.';
$len_str04 = strlen($str04);
$needle04 = 'HE';
$offset04 = 0;
$length04 = $len_str04;
$countneedle04 = substr_count($str04,
$needle04,
$offset04,
$length04);
echo "There are $countneedle04
occurrences of '$needle04' on STRING:<br>
$str04<br>";
echo '<br>Offset = ' . $offset04 .
'<br>Length = ' . $length04 . '<br>';
?>
EXERCISE
<?php
echo "Testing basic operations.<br>";
try {
substr_count("", "");
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
substr_count("a", "");
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
var_dump(substr_count("", "a"));
echo '<br><br>';
var_dump(substr_count("", "a"));
echo '<br><br>';
var_dump(substr_count("", chr(0)));
echo '<br><br><br>';
$a = str_repeat("abcacba", 100);
var_dump(substr_count($a, "bca"));
echo '<br><br><br>';
$a = str_repeat("abcacbabca", 100);
var_dump(substr_count($a, "bca"));
echo '<br><br>';
var_dump(substr_count($a, "bca", 200));
echo '<br><br>';
var_dump(substr_count($a, "bca", 200, null));
echo '<br><br>';
var_dump(substr_count($a, "bca", 200, 50));
echo '<br><br>';
var_dump(substr_count($a, "bca", -200));
echo '<br><br>';
var_dump(substr_count($a, "bca", -200, null));
echo '<br><br>';
var_dump(substr_count($a, "bca", -200, 50));
echo '<br><br>';
var_dump(substr_count($a, "bca", -200, -50));
?>