The second parameter is a set of allowed characters.
strspn will return an zero-based index of a first non-allowed character.
strspn
(PHP 4, PHP 5)
strspn — Finds the length of the first segment of a string consisting entirely of characters contained within a given mask.
Descrierea
Returns the length of the first group of consecutive characters from mask found in subject .
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:
<?php
$var = strspn("42 is the answer to the 128th question.", "1234567890");
?>
will assign 2 to $var, because the string "42" is the first segment from subject to consist only of characters contained within "1234567890".
Parametri
- subject
-
The string to examine.
- mask
-
The list of allowable characters to include in counted segments.
- 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 .
Valorile întroarse
Returns the length of the initial segment of str1 which consists entirely of characters in str2 .
Istoria schimbărilor
| Versiunea | Descriere |
|---|---|
| 4.3.0 | The start and length parameters were added |
Exemple
Example #1 strspn() example
<?php
echo strspn("foo", "o", 1, 2); // prints: 2
?>
Note
Notă: Această funcţie acceptă şi date binare.
strspn
04-Mar-2009 06:23
08-Aug-2008 09:12
It took me some time to understand the way this function works…
I’ve compiled my own explanation with my own words that is more understandable for me personally than the official one or those that can be found in different tutorials on the web.
Perhaps, it will save someone several minutes…
<?php
strspn(string $haystack, string $char_list [, int $start [, int $length]])
?>
The way it works:
- searches for a segment of $haystack that consists entirely from supplied through the second argument chars
- $haystack must start from one of the chars supplied through $char_list, otherwise the function will find nothing
- as soon as the function encounters a char that was not mentioned in $chars it understands that the segment is over and stops (it doesn’t search for the second, third and so on segments)
- finally, it measures the segment’s length and return it (i.e. length)
In other words it finds a span (only the first one) in the string that consists entirely form chars supplied in $chars_list and returns its length
03-Oct-2007 12:20
This function is significantly faster for checking illegal characters than the equivalent preg_match() method.
