PHP strspn() Function
The PHP strspn() function returns the length of the initial segment of a string consisting entirely of characters contained within a specified charlist.
If offset and length are omitted, then all of string will be searched. If they are included, the portion of string specified by the offset and length parameters will be searched.
Note: The strspn() function is a binary-safe.
Syntax
strspn(string, charlist, offset, length)
Parameters
string |
Required. Specify the string to search. |
charlist |
Required. Specify the list of allowed characters to search for. |
offset |
Optional. Specify offset parameter which indicates the starting position in the string.
|
length |
Optional. Specify the length of the segment from string to examine. Default is to the end of the string.
|
Return Value
Returns the length of the initial segment of the string consisting entirely of characters contained within charlist.
Example: strspn() example
The example below shows the usage of strspn() function.
<?php //length of initial segment of string //containing characters from 'LEO' echo strspn("HELLO WORLD!", "LEO")."\n"; //length of initial segment of string //containing characters from 'LEOH' echo strspn("HELLO WORLD!", "LEOH")."\n"; //length of initial segment of string //starting at offset 1 and length 7 //which contains characters from 'LEO' echo strspn("HELLO WORLD!", "LEO", 1, 7)."\n"; //length of initial segment of string //starting at offset 1 and length 2 //which contains characters from 'LEO' echo strspn("HELLO WORLD!", "LEO", 1, 2)."\n"; ?>
The output of the above code will be:
0 5 4 2
❮ PHP String Reference