PHP strspn() Function (original) (raw)

Last Updated : 11 Jul, 2025

The strspn() function is an in-built function in PHP which finds the length of the initial segment of a string consisting entirely of characters contained within a given list of characters passed as a parameter to the function.Syntax :

strspn( string,string, string,charlist, start,start, start,length)

Parameters : This function accepts four parameters as shown in the above syntax. First two parameters are mandatory and must be supplied while the remaining two parameters are optional. All of these parameters are described below:

Return Value: This function returns the number of characters found in the string that contains only characters from the charlist parameter. Examples:

Input : string="abcdefghijk",string = "abcdefghijk", string="abcdefghijk",charlist = "abcjkl" Output : 3

Input : string="GeeksforGeeks",string = "Geeks for Geeks", string="GeeksforGeeks",charlist = "Geeksfor " Output : 15

Below programs illustrate the strspn() function:Program 1:

php `

`

Output:

15

Program 2: This program illustrates the case-sensitivity of strspn() function.

php `

`

Output:

0

Program 3: This program illustrates the use of strspn() function with startandstart and startandlength parameters.

php `

`

Output:

5

Program 4: This program illustrates the use of strspn() function with negative $length parameter.

php `

`

Output:

5

Program 5: This program illustrates the use of strspn() function with a negative $start parameter.

php `

`

Output:

0

Reference: https://www.php.net/manual/en/function.strspn.php