strcspn
Description
Syntax
strcspn( string $string, string $characters, int $offset = 0, ?int $length = null ): int
Parameters
string
The string to examine.
characters
The string containing every disallowed character.
offset
The position in string to start searching.
If offset is given and is non-negative, then strcspn() 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 strcspn() 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 not in characters.
NOTE: When a start parameter is set, the returned length is counted starting from this position, not from the beginning of string.
Examples
1 · string characters
<? $string = 'abcdefghijklmnopqrstu'; $characters = 'no'; $return = strcspn($string, $characters); echo $return;
13
2 · offset · negative
<? $string = 'abcdefghijklmnopqrstu'; $characters = 'no'; $offset = -10; $return = strcspn($string, $characters, $offset); echo $return;
2
3 · offset · non-negative
<? $string = 'abcdefghijklmnopqrstu'; $characters = 'no'; $offset = 10; $return = strcspn($string, $characters, $offset); echo $return;
3
4 · length · negative
<? $string = 'abcdefghijklmnopqrstu'; $characters = 'no'; $offset = 10; $length = -10; $return = strcspn($string, $characters, $offset, $length); echo $return;
1
5 · length · non-negative
<? $string = 'abcdefghijklmnopqrstu'; $characters = 'no'; $offset = 10; $length = 10; $return = strcspn($string, $characters, $offset, $length); echo $return;
3
Links
String
- 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
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1
- sha1_file
- similar_text
- soundex
- sprintf
- sscanf
- str_contains
- str_decrement
- str_ends_with
- str_getcsv
- str_increment
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_starts_with
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr
- substr_compare
- substr_count
- substr_replace
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap