PHP | strcspn() Function

The strcspn() function is an in-built function in PHP which returns the number of characters present in a string before any part of the specified characters to be searched are found. This function is case-sensitive.

Syntax :

strcspn( $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 to start searching in the string.
    • If $start is given and is non-negative, then strcspn() will begin examining $string from that position.
    • If $start is given and is negative, then strcspn() 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: Returns the number of characters from the starting position (including whitespaces) present in the string before any of the characters from the $charlist parameter is found in the string.

Examples:

Input : $string = "Geeks for Geeks", $charlist = "mnopqr"
Output : 7

Input : $string = "Geeks for Geeks", $charlist = "for"
Output : 6

Below programs will illustrate the use of strcspn() function :

Program 1 : This program shows simple use of strcspn() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Output is 6 because the input string
// contains 6 characters "Geeks " before 
// the first character 'f' from the list
// "for" is found in the string.
echo strcspn("Geeks for Geeks", "for"); 
  
?>

chevron_right


Output:

6

Program 2 : This program shows case-sensitivity of strcspn() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Output is 7 because the input string
// does not contain 'F' as specified in the list "For".
// Hence the first character from the
// list that is present in the string is 'o'
echo strcspn("Geeks for Geeks", "For"); 
  
?>

chevron_right


Output:

7

Program 3: This program shows use of strcspn() function with $start parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Searches from index 5 till
// the end of the string
echo strcspn("Geeks for Geeks", "G", 5); 
  
?>

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. Output is 0 since the character
// at $start (i.e. 5) is present in the 
// specified list of characters
echo strcspn("Geeks for Geeks", " for", 5, -5);
  
?> 

chevron_right


Output:

0

Program 5: This program shows use of strcspn() function with negative $start parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Searches from 5th index from the end of the string
// Output is 0 as the character 'G' in the 
// specified starting index is present in the
// given list of characters to be checked.
echo strcspn("Geeks for Geeks", "Geek", -5);
   
?>

chevron_right


Output:

0

Reference:
http://php.net/manual/en/function.strcspn.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.