THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals

PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP JSON

PHP OOP

PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Traits PHP Static Methods PHP Static Properties

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP Examples

PHP Examples PHP Quiz PHP Exercises PHP Certificate

PHP Reference

PHP Overview PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP substr_count() Function

❮ PHP String Reference

Example

Count the number of times "world" occurs in the string:

<?php
echo substr_count("Hello world. The world is nice","world");
?>
Try it Yourself »

The substr_count() function counts the number of times a substring occurs in a string.

Note: The substring is case-sensitive.

Note: This function does not count overlapped substrings (see example 2).

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 Values

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. If negative, it starts counting from the end of the string
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: PHP 7.1 - The length parameters can be 0 or a negative number.
PHP 7.1 - The start parameters can be a negative number.
PHP 5.1 - The start and length parameters were added.

More Examples

Example

Using all parameters:

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

Example

Overlapped substrings:

<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Try it Yourself »

Example

If the start and length parameters exceeds the string length, this function will output a warning:

<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>

This will output a warning because the length value exceeds the string length (3+9 is greater than 12)


❮ PHP String Reference