PHP strspn() Function

The strspn() function returns the number of characters found in the string that contains only characters from the charlist parameter.

int strspn ( string $subject , string $mask [, int $start [, int $length ]] )

Finds the length of the initial segment of subject that contains only characters from mask.

If start and length are omitted, then all of subject will be examined. If they are included, then the effect will be the same as calling strspn(substr($subject, $start, $length), $mask) (see substr for more information).

The line of code:

will assign 2 to $var, because the string "42" is the initial segment of subject that consists only of characters contained within "1234567890".

Example -

The above example will output:

int(0)

int(2)

int(1)

ParameterDescription
stringRequired. Specifies the string to search
charlistRequired. Specifies the characters to find
startOptional. Specifies where in the string to start
lengthOptional. Defines the length of the string

Returns the length of the initial segment of subject which consists entirely of characters in mask.