PHP strcspn() Function (original) (raw)
Last Updated : 21 Jun, 2023
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,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:
- $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 startisgivenandis∗∗non−negative∗∗,thenstrcspn()willbeginexaminingstart is given and is non-negative, then strcspn() will begin examining startisgivenandis∗∗non−negative∗∗,thenstrcspn()willbeginexaminingstring from that position.
- If startisgivenandis∗∗negative∗∗,thenstrcspn()willbeginexaminingstart is given and is negative, then strcspn() will begin examining startisgivenandis∗∗negative∗∗,thenstrcspn()willbeginexaminingstring from that position from the end of $string.
- $length : It specifies the number of characters of stringwhichareneededtobesearched.Itsdefaultvalueistilltheendofthestring which are needed to be searched. Its default value is till the end of the stringwhichareneededtobesearched.Itsdefaultvalueistilltheendofthestring.
- If lengthisgivenandis∗∗non−negative∗∗,thenlength is given and is non-negative, then lengthisgivenandis∗∗non−negative∗∗,thenstring will be examined for $length characters from the starting position.
- If lengthisgivenandis∗∗negative∗∗,thenlength is given and is negative, then lengthisgivenandis∗∗negative∗∗,thenstring will be examined from the starting position up to lengthcharactersfromtheendoflength characters from the end of lengthcharactersfromtheendofstring.
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="GeeksforGeeks",string = "Geeks for Geeks", string="GeeksforGeeks",charlist = "mnopqr" Output : 7
Input : string="GeeksforGeeks",string = "Geeks for Geeks", string="GeeksforGeeks",charlist = "for" Output : 6
Below programs will illustrate the use of strcspn() function :
Program 1 : This program shows simple use of strcspn() function.
PHP
<?php
echo
strcspn
(
"Geeks for Geeks"
,
"for"
);
?>
Output:
6
Program 2 : This program shows case-sensitivity of strcspn() function.
PHP
<?php
echo
strcspn
(
"Geeks for Geeks"
,
"For"
);
?>
Output:
7
Program 3: This program shows use of strcspn() function with $start parameter.
PHP
<?php
echo
strcspn
(
"Geeks for Geeks"
,
"G"
, 5);
?>
Output:
5
Program 4: This program illustrates the use of strcspn() function with negative $length parameter.
PHP
<?php
echo
strcspn
(
"Geeks for Geeks"
,
" for"
, 5, -5);
?>
Output:
0
Program 5: This program shows use of strcspn() function with negative $start parameter.
php
<?php
echo
strcspn
(
"Geeks for Geeks"
,
"Geek"
, -5);
?>
Output:
0