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).
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…