Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

strspn

Description

The strspn String for PHP finds the length of the initial segment of a string consisting entirely of characters contained within a given mask.

Syntax

strspn(
    string $string,
    string $characters,
    int $offset = 0,
    ?int $length = null
): int

Parameters

string

The string to examine.

characters

The list of allowable characters.

offset

The position in string to start searching.

If offset is given and is non-negative, then strspn() will begin examining string at the offset'th position. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If offset is given and is negative, then strspn() will begin examining string at the offset'th position from the end of string.

length

The length of the segment from string to examine.

If length is given and is non-negative, then string will be examined for length characters after 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

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

NOTE: When a offset parameter is set, the returned length is counted starting from this position, not from the beginning of string.

Examples

1 · string characters

<?

$string = '01234allow56789';
$characters = 'allow';

$return = strspn($string, $characters);

echo $return;

?>
0

2 · offset · negative

<?

$string = '01234allow56789';
$characters = 'allow';
$offset = -10;

$return = strspn($string, $characters, $offset);

echo $return;

?>
5

3 · offset · non-negative

<?

$string = '01234allow56789';
$characters = 'allow';
$offset = 5;

$return = strspn($string, $characters, $offset);

echo $return;

?>
5

4 · length · negative

<?

$string = '01234allow56789';
$characters = 'allow';
$offset = 5;
$length = -5;

$return = strspn($string, $characters, $offset, $length);

echo $return;

?>
5

5 · length · non-negative

<?

$string = '01234allow56789';
$characters = 'allow';
$offset = 5;
$length = 5;

$return = strspn($string, $characters, $offset, $length);

echo $return;

?>
5
HomeMenu