Javatpoint Logo
Javatpoint Logo

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.

Syntax

The syntax for the strspn() is given below, which consist of four parameters.

Parameter

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.

  • If $start is given and it is a non-negative value, then searching will begin from that position which is given in the $start parameter.
  • If $start is given and contain a negative value, then strspn() function will start searching of $charsearch from the end of that position in the $string.

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

  • If the $length parameter is given and having a positive value, then the $charsearch will be searched in the $string from beginning to the $length value.
  • If the $length parameter is given and has a negative value, then the $charsearch will be searched from start position up to the $length character from the end of the $string.

Return Values

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.

Examples

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

Example 1

This is the basic and very simple example of strspn() function.

Output:

4

Example 2

Output:

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

Example 3

Output:

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

Output:

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

Example 5

Output:

In this example, strspn() function returned 0, because AB did not match with the main string.

Number of characters matched = 0





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA