substr_count() counts non-overlapping occurrences of a substring, optionally within a selected range of the main string.
$text = 'one two one three one';
echo substr_count($text, 'one'); // 3
$my_string="PHP is a server side script and learn PHP at www.plus2net.com";
echo substr_count($my_string,"PHP");
The above code will print 2
substr_count(string $haystack,string $needle,int $offset, int $length);
| Parameter | DESCRIPTION |
|---|---|
| $haystack | Required : Input string |
| $needle | Required : substring to be searched within Input string |
| $offset | Optional : Where to start counting. If negative (in PHP 7 only ) then from end of the input string |
| $length | Optional : The maximum length after the $offset to search for the substring. If negative ( in PHP 7 only ) then count from end of the haystack ( input string ) only. Sum of offset and $length should not be more than the lenght of $haystack ( Warning as output ) |

$string="Welcome to plus2net.com, you can add your comment";
echo strlen($string); // Output is 49 ( length of the string )
echo substr_count($string,'com'); // Output is 3
echo substr_count($string,'com',3); // Output is 3
echo substr_count($string,'com',4); // Output is 2
echo substr_count($string,'com',20); // Output is 2
echo substr_count($string,'com',21); // Output is 1
echo substr_count($string,'com',3,3); // Output is 1
echo substr_count($string,'com',4,3); // Output is 0
echo substr_count($string,'com',0,10); // Output is 1
echo substr_count($string,'com',4,18); // Output is 0
echo substr_count($string,'com',4,19); // Output is 1
echo substr_count($string,'com',20); // Output is 2
echo substr_count($string,'com',20,29); // Output is 2
// The invalid call substr_count($string, 'com', 20, 30) throws ValueError
// because 20 + 30 exceeds the 49-byte haystack length.
$string="Welcome to plus2net.com, you can add your comment";
//echo strlen($string); // Output is 49 ( length of the string )
// Negative offsets and lengths (PHP 7.1+)
echo substr_count($string,'com',-49); // Output is 3
echo substr_count($string,'com',-49,-4); // Output is 3
echo substr_count($string,'com',-49,-5); // Output is 2
echo substr_count($string,'com',-47,-4); // Output is 3
echo substr_count($string,'com',-49,-7); // Output is 2
echo substr_count($string,'com',-49,-26);// Output is 2
echo substr_count($string,'com',-49,-27);// Output is 1
echo substr_count($string,'com',-29,-26);// Output is 1
echo substr_count($string,'com',-28,-26);// Output is 0
substr_count() can count @ symbols, but that alone cannot establish whether an email address is valid. For submitted email addresses, validate the complete input with filter_var() and FILTER_VALIDATE_EMAIL. The next example demonstrates the distinction.
$email = "user@example.com";
$atCount = substr_count($email, "@"); // Character count only
$isValid = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
if ($isValid) {
echo "Valid email format";
}
Validation of an address does not establish that the mailbox exists or accepts delivery.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Paul, copySnips.com | 02-09-2009 |
| Thanks for sharing this example. Very useful. | |
| John | 13-10-2014 |
| Very useful thanks! | |