substr
Description
Syntax
substr( string $string, int $offset, ?int $length = null ): string
Parameters
string
The input string.
offset
If offset is non-negative, the returned string will start at the offset'th position in string, counting from zero. 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 negative, the returned string will start at the offset'th character from the end of string.
If string is less than offset characters long, an empty string will be returned.
length
If length is given and is positive, the string returned will contain at most length characters beginning from offset (depending on the length of string).
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a offset is negative). If offset denotes the position of this truncation or beyond, an empty string will be returned.
If length is given and is 0, an empty string will be returned.
If length is omitted or null, the substring starting from offset until the end of the string will be returned.
Return
Returns the extracted part of string, or an empty string.
Examples
1 · string offset · negative
<? $string = 'abcde'; $offset1 = -1; $offset2 = -5; for($i = $offset1; $offset2 <= $i; --$i) { $return = substr($string, $i); echo "offset:$i return:$return\n"; }
offset:-1 return:e offset:-2 return:de offset:-3 return:cde offset:-4 return:bcde offset:-5 return:abcde
2 · string offset · non-negative
<? $string = 'abcde'; $offset1 = 0; $offset2 = 4; for($i = $offset1; $i <= $offset2; ++$i) { $return = substr($string, $i); echo "offset:$i return:$return\n"; }
offset:0 return:abcde offset:1 return:bcde offset:2 return:cde offset:3 return:de offset:4 return:e
3 · length · negative
<? $string = 'abcde'; $offset1 = 0; $offset2 = 4; $length1 = -1; $length2 = -5; for($i = $offset1; $i <= $offset2; ++$i) { for($j = $length1; $length2 <= $j; --$j) { $return = substr($string, $i, $j); echo "offset:$i length:$j return:$return\n"; } }
offset:0 length:-1 return:abcd offset:0 length:-2 return:abc offset:0 length:-3 return:ab offset:0 length:-4 return:a offset:0 length:-5 return: offset:1 length:-1 return:bcd offset:1 length:-2 return:bc offset:1 length:-3 return:b offset:1 length:-4 return: offset:1 length:-5 return: offset:2 length:-1 return:cd offset:2 length:-2 return:c offset:2 length:-3 return: offset:2 length:-4 return: offset:2 length:-5 return: offset:3 length:-1 return:d offset:3 length:-2 return: offset:3 length:-3 return: offset:3 length:-4 return: offset:3 length:-5 return: offset:4 length:-1 return: offset:4 length:-2 return: offset:4 length:-3 return: offset:4 length:-4 return: offset:4 length:-5 return:
4 · length · non-negative
<? $string = 'abcde'; $offset1 = 0; $offset2 = 4; $length1 = 0; $length2 = 4; for($i = $offset1; $i <= $offset2; ++$i) { for($j = $length1; $j <= $length2; ++$j) { $return = substr($string, $i, $j); echo "offset:$i length:$j return:$return\n"; } }
offset:0 length:0 return: offset:0 length:1 return:a offset:0 length:2 return:ab offset:0 length:3 return:abc offset:0 length:4 return:abcd offset:1 length:0 return: offset:1 length:1 return:b offset:1 length:2 return:bc offset:1 length:3 return:bcd offset:1 length:4 return:bcde offset:2 length:0 return: offset:2 length:1 return:c offset:2 length:2 return:cd offset:2 length:3 return:cde offset:2 length:4 return:cde offset:3 length:0 return: offset:3 length:1 return:d offset:3 length:2 return:de offset:3 length:3 return:de offset:3 length:4 return:de offset:4 length:0 return: offset:4 length:1 return:e offset:4 length:2 return:e offset:4 length:3 return:e offset:4 length:4 return:e
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
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap