PHP | strspn() Function

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, $charlist, $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:

  • $string : This mandatory parameter specifies the string to search.
  • $charlist : This mandatory parameter specifies the list of characters to be searched in the given $string.
  • $start : This optional parameter specifies the index from where we want to start searching in the string.
    • If $start is given and is non-negative, then strspn() will begin examining $string from that position.
    • If $start is given and is negative, then strspn() will begin examining $string from that position from the end of $string.
  • $length : It specifies the number of characters of $string which are needed to be searched. Its default value is till the end of the $string.
    • If $length is given and is non-negative, then $string will be examined for $length characters from the starting position.
    • If $length is given and is negative, then $string will be examined from the starting position up to $length characters from the end of $string.

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", $charlist = "abcjkl"
Output : 3

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

Below programs illustrate the strspn() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Output is 15 because whole input string
// contains all characters from given char list
// "Geeksfor "
echo strspn("Geeks for Geeks", "Geeksfor ");
?> 

chevron_right


Output:

15

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Output is 0 because there is no substring
// which contains all characters of given char
// list.
echo strspn("Geeks for Geeks", "geeks");
?> 

chevron_right


Output:

0

Program 3: This program illustrates the use of strspn() function with $start and $length parameters.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Searches substring starting from index 5 
// and length 9 with all characters in char 
// list " for"
echo strspn("Geeks for Geeks", " for", 5, 9);
?> 

chevron_right


Output:

5

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Searches from index 5 till 5-th position from
// end.
echo strspn("Geeks for Geeks", " for", 5, -5);
?> 

chevron_right


Output:

5

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Searches from 5-th index from end
echo strspn("Geeks for Geeks", "for", -5);
?> 

chevron_right


Output:

0

Reference:
http://php.net/manual/en/function.strspn.php



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.