Note: This function generates a warning if the start
parameter plus the length parameter is greater than the string length (see example 3).

Syntax

substr_count(string,substring,start,length)

Parameter

Description

string

Required. Specifies the string to check

substring

Required. Specifies the string to search for

start

Optional. Specifies where in string to start searching

length

Optional. Specifies the length of the search

Technical Details

Return Value:

Returns the the number of times the substring occurs
in the string

PHP Version:

4+

Changelog:

The start and length parameters were added in PHP 5.1

More Examples

Example 1

Using all parameters:

<?php
$str = "This is nice";echo strlen($str)."<br>"; // Using strlen() to
return the string lengthecho substr_count($str,"is")."<br>"; // The
number of times "is" occurs in the stringecho substr_count($str,"is",2)."<br>";
// The string is now reduced to "is is PHP"echo substr_count($str,"is",3)."<br>";
// The string is now reduced to "s is PHP"echo substr_count($str,"is",3,3)."<br>";
// The string is now reduced to "s i"
?>

Web Certificates

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy.
Copyright 1999-2015 by Refsnes Data. All Rights Reserved.