PHP strspn() Function

PHP strspn() Function

The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter.

Syntax

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

Parameter

subject(required)- This parameter represents the input string.

mask(required)- This parameter specifies the characters to find.

start (optional)- It signifies the position in the string to start searching.

length(optional)- This parameter represents the length of the segment from subject to examine. If length is positive, then string will be examined for length characters after the starting position else for negative length the subject will be examined from the starting position.

Return

This function returns the length of the initial segment of subject only characters from the mask parameter.

Example 1

  

Output

String 1: Hello world!
String 2: PHPlleo
The strspn() function will return: 5 

Example 2

  

Output

 String 1: Hook
 String 2: o
 The returned value: int(0) 

Example 3

  

Output

String 1: Hook
String 2: o
The returned value: int(2) 

Example 4

  

Output

String 1: Hook
String 2: o
The returned value: int(2)