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.
[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, Optional, String, Specifies where to start searching in string.
length, Optional, String, it tells length of the search.
[/table]
Returns the the number of times the substring occurs in the string.
[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]
<?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:
<?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:
<?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:
<?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
<?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).
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…