PHP String strspn() function
The strspn() function is an in-built function of PHP. This function is used to find the length of the initial segment of a string placed inside another string. In simple words - the strspn() function helps us to find "how many number of characters presents inside another string?" It returns the total number of characters found in the string that contains only characters from the $charsearch parameter.
It is a case-sensitive function which treats uppercase and lowercase characters differently.
The syntax for the strspn() is given below, which consist of four parameters.
The strspn() function consists four parameters as shown in the above syntax. In this, two parameters are mandatory while the remaining two parameters are optional. The detailed information for all these parameters is given below:
$string (mandatory): It is a mandatory string parameter in which we find the search string. This parameter specifies the string to be searched.
$charsearch (mandatory): It is also a compulsory parameter, which specifies a list of characters to search in the given $string parameter.
$start (optional): This parameter is optional which specifies that from where we want to start the searching in the $string parameter. This parameter holds an integer value.
$length (optional): This parameter is an optional parameter which defines the number of characters to be searched in the $string. If the $length parameter is not given then, by default it examines the whole $string.
The strspn() function returns the total number of characters initially found in the $string, which is contained by the $charsearch parameter.
It returns 0 if any character of $charsearch is not present in the $string.
Note: The length and start parameters were supported by PHP 4.3 and above versions.
Below some examples are given which will help you to understand the working of the strspn() function more easily and quickly.
Input $string = ABCDEFGHIJKL, $charsearch = ABCDGHI Output = 4 Input $string = ABCDEFGHIJKL, $charsearch = abc //case-sensitive Output = 0 Input $string = ABCDEFGHIJKL, $charsearch = ABCDE, start = 2 Output = 3
This is the basic and very simple example of strspn() function.
In this example, we did not provide the start and length parameter so it starts searching from beginning to the end of the string. It is found that 3 characters matched with the main string.
Number of characters matched = 3
In this example, the whole $search_str1 string is found in main string, so it returned 13 matching characters. This function finds "Good Morning!" and "Morning! Good" similar.
Number of characters matched = 13
Example 4: Case-Sensitive
This function has returned 1 because it treated "Good Morning!" and "GOOD MORNING!" differently except G. Only first letter of both strings is matched with each other. It proved that the following function is case-sensitive.
Number of character matches = 1
In this example, strspn() function returned 0, because AB did not match with the main string.
Number of characters matched = 0