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).

You may also like...