PHP.nl

strspn

strspn

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

int **strspn** string $string string $characters int $offset  $length

Finds the length of the initial segment of that contains characters from . stringonlycharacters

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

The line of code:

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

<?php
$var = strspn("42 is the answer to the 128th question.", "1234567890");
?>

2``$var``string

stringThe string to examine.

charactersThe list of allowable characters.

offset The position in to start searching. string

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


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

length The length of the segment from to examine. string

   If  is given and is non-negative,
   then  will be examined
   for  characters after the starting
   position.
  `length``string``length`


    If  is given and is negative,
    then  will be examined from the
    starting position up to 
    characters from the end of .
  `length``string``length``string`

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

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

Voorbeeld: example

<?php
// subject does not start with any characters from mask
var_dump(strspn("foo", "o"));

// examine two characters from subject starting at offset 1
var_dump(strspn("foo", "o", 1, 2));

// examine one character from subject starting at offset 1
var_dump(strspn("foo", "o", 1, 1));
?>
int(0)
int(2)
int(1)

strcspn