Categories: PHP

substr_count – PHP String Functions

Syntax :

substr_count ( string, substring, start_position, length );

Description :

It’s an inbuilt function of PHP. substr_count() function counts the number of times a substring comes in a string.

Note: The substring is case-sensitive. and this function does not count overlapped substrings.

Note: This function generates a warning if the start parameter plus the length parameter is greater than the string length.


Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, Main string to check into.

substring, Required, String, String to be searched for.

start_position, OptionalString,  Specifies where to start searching in string.

length, OptionalString, it tells length of the search.

[/table]


Output :

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


ChangeLog :

[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 7.1.0, Support for negative start_position and length has been added. length may also be 0 now.
PHP 5.1.0, Added the start_position and length parameters.
[/table]


Related articles : count_chars() , str_word_count().


substr_compare() – PHP Functions Example 1 :
<?php
echo substr_count("Hi from Tutorialmines. you get all Tutorial here","Tutorial"); 
?>

In above example, We have string “Hi from Tutorialmines. you get all Tutorial here” and substring2 is “Tutorial”. Substring comes 2 times in the string. So, the output of above code in the browser is as below:

2

substr_count() – PHP Functions Example 2 :  string case sensitive count of string “tutorial”.
<?php
echo substr_count("Hi from Tutorialmines. you get all tutorial here","tutorial",); 
?>

In above example, We have string “Hi from Tutorialmines. you get all tutorial here” and substring2 is “tutorial”. Substring comes 1 times in the string Notice the case sensitiveness of function. So, the output of above code in the browser is as below:

1

substr_count() – PHP Functions Example 3 :  it doesn’t count the overlapped strings.
<?php
// It will output only 1, because it doesn't count overlapped substrings
$str1 = 'xyzxyzxyz';
echo substr_count($str1, 'xyzxyz'); 
?>

In above example, it doesn’t count overlapped substrings. Output of above code in the browser is as below:

1

substr_count() – PHP Functions Example 4 :  Using all possible parameters
<?php
$str = "This is my site.";
// strlen() function will return the string length
echo strlen($str)."<br>\n";

// Number of times "is" occurs in the string
echo substr_count($str,"is")."<br>\n";

// The string is now reduced to "is is my site."
echo substr_count($str,"is",2)."<br>\n";

// The string is now reduced to "s is my site."
echo substr_count($str,"is",3)."<br>\n";

// The string is now reduced to "s i"
echo substr_count($str,"is",3,3)."<br>\n";
?>

Output of above code is :

16
2
2
1
0


substr_count() – PHP Functions Example 5 :  If the start and length parameters exceeds the string length, this function will output a warning:
<?php
echo $str = "This is cool site.";
echo "\n".strlen($str);
echo "\n".substr_count($str,"is",3,20);
?>

This will output a warning because the length value exceeds the string length (3+20 is greater than 23).

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

2 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

2 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

2 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

2 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

2 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

2 months ago