strspn

Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask

Syntax

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

Parameters

subject

The string to examine.

mask

The list of allowable characters.

start

The position in subject to start searching. If start is given and is non-negative, then strspn() will begin examining subject at the start'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 start is given and is negative, then strspn() will begin examining subject at the start'th position from the end of subject.

length

The length of the segment from subject to examine. If length is given and is non-negative, then subject will be examined for length characters after the starting position. If length is given and is negative, then subject will be examined from the starting position up to length characters from the end of subject.

Return

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

Note: When a start parameter is set, the returned length is counted starting from this position, not from the beginning of subject.

Examples

1 · subject mask

<?

$subject = "12345abcde12345abcde";
$mask = "abcde";

$return = strspn($subject, $mask);

echo $return;

?>
0

2 · start · Negative

<?

$subject = "12345abcde12345abcde";
$mask = "abcde";
$start = -15;

$return = strspn($subject, $mask, $start);

echo $return;

?>
5

3 · start · Non-negative

<?

$subject = "12345abcde12345abcde";
$mask = "abcde";
$start = 5;

$return = strspn($subject, $mask, $start);

echo $return;

?>
5

4 · length · Negative

<?

$subject = "12345abcde12345abcde";
$mask = "abcde";
$start = -15;
$length = -10;

$return = strspn($subject, $mask, $start, $length);

echo $return;

?>
5

5 · length · Non-negative

<?

$subject = "12345abcde12345abcde";
$mask = "abcde";
$start = 5;
$length = 5;

$return = strspn($subject, $mask, $start, $length);

echo $return;

?>
5

addcslashes

addslashes

bin2hex

chop

chr

chunk_split

convert_uudecode

convert_uuencode

count_chars

crc32

crypt

echo

explode

fprintf

get_html_translation_table

hebrev

hebrevc

hex2bin

html_entity_decode

htmlentities

htmlspecialchars

htmlspecialchars_decode

implode

join

lcfirst

levenshtein

localeconv

ltrim

md5

md5_file

metaphone

nl_langinfo

nl2br

number_format

ord

parse_str

print

printf

quoted_printable_decode

quoted_printable_encode

quotemeta

rtrim

setlocale

sha1

sha1_file

similar_text

soundex

sprintf

sscanf

str_contains

str_ends_with

str_getcsv

str_ireplace

str_pad

str_repeat

str_replace

str_rot13

str_shuffle

str_split

str_starts_with

str_word_count

strcasecmp

strchr

strcmp

strcoll

strcspn

strip_tags

stripcslashes

stripos

stripslashes

stristr

strlen

strnatcasecmp

strnatcmp

strncasecmp

strncmp

strpbrk

strpos

strrchr

strrev

strripos

strrpos

strstr

strtok

strtolower

strtoupper

strtr

substr

substr_compare

substr_count

substr_replace

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

PHP

Home Menu