substr

Return part of a string

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, FALSE 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, FALSE will be returned. If length is given and is 0, FALSE or NULL, an empty string will be returned. If length is omitted, 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 = -5;
$offset2 = -1;

for($i = $offset1; $i <= $offset2; ++$i)
{
    echo "offset:$i return:" . substr($string, $i) . PHP_EOL;
}

?>
offset:-5 return:abcde
offset:-4 return:bcde
offset:-3 return:cde
offset:-2 return:de
offset:-1 return:e

2 · string offset · Non-negative

<?

$string = "abcde";
$offset1 = 0;
$offset2 = 4;

for($i = $offset1; $i <= $offset2; ++$i)
{
    echo "offset:$i return:" . substr($string, $i) . PHP_EOL;
}

?>
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 = -5;
$offset2 = 4;
$length1 = -5;
$length2 = -1;

for($i = $offset1; $i <= $offset2; ++$i)
{
    for($j = $length1; $j <= $length2; ++$j)
    {
        echo "offset:$i length:$j return:" . substr($string, $i, $j) . PHP_EOL;
    }
}

?>
offset:-5 length:-5 return:
offset:-5 length:-4 return:a
offset:-5 length:-3 return:ab
offset:-5 length:-2 return:abc
offset:-5 length:-1 return:abcd
offset:-4 length:-5 return:
offset:-4 length:-4 return:
offset:-4 length:-3 return:b
offset:-4 length:-2 return:bc
offset:-4 length:-1 return:bcd
offset:-3 length:-5 return:
offset:-3 length:-4 return:
offset:-3 length:-3 return:
offset:-3 length:-2 return:c
offset:-3 length:-1 return:cd
offset:-2 length:-5 return:
offset:-2 length:-4 return:
offset:-2 length:-3 return:
offset:-2 length:-2 return:
offset:-2 length:-1 return:d
offset:-1 length:-5 return:
offset:-1 length:-4 return:
offset:-1 length:-3 return:
offset:-1 length:-2 return:
offset:-1 length:-1 return:
offset:0 length:-5 return:
offset:0 length:-4 return:a
offset:0 length:-3 return:ab
offset:0 length:-2 return:abc
offset:0 length:-1 return:abcd
offset:1 length:-5 return:
offset:1 length:-4 return:
offset:1 length:-3 return:b
offset:1 length:-2 return:bc
offset:1 length:-1 return:bcd
offset:2 length:-5 return:
offset:2 length:-4 return:
offset:2 length:-3 return:
offset:2 length:-2 return:c
offset:2 length:-1 return:cd
offset:3 length:-5 return:
offset:3 length:-4 return:
offset:3 length:-3 return:
offset:3 length:-2 return:
offset:3 length:-1 return:d
offset:4 length:-5 return:
offset:4 length:-4 return:
offset:4 length:-3 return:
offset:4 length:-2 return:
offset:4 length:-1 return:

4 · length · Non-negative

<?

$string = "abcde";
$offset1 = -5;
$offset2 = 4;
$length1 = 0;
$length2 = 4;

for($i = $offset1; $i <= $offset2; ++$i)
{
    for($j = $length1; $j <= $length2; ++$j)
    {
        echo "offset:$i length:$j return:" . substr($string, $i, $j) . PHP_EOL;
    }
}

?>
offset:-5 length:0 return:
offset:-5 length:1 return:a
offset:-5 length:2 return:ab
offset:-5 length:3 return:abc
offset:-5 length:4 return:abcd
offset:-4 length:0 return:
offset:-4 length:1 return:b
offset:-4 length:2 return:bc
offset:-4 length:3 return:bcd
offset:-4 length:4 return:bcde
offset:-3 length:0 return:
offset:-3 length:1 return:c
offset:-3 length:2 return:cd
offset:-3 length:3 return:cde
offset:-3 length:4 return:cde
offset:-2 length:0 return:
offset:-2 length:1 return:d
offset:-2 length:2 return:de
offset:-2 length:3 return:de
offset:-2 length:4 return:de
offset:-1 length:0 return:
offset:-1 length:1 return:e
offset:-1 length:2 return:e
offset:-1 length:3 return:e
offset:-1 length:4 return:e
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

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

strspn

strstr

strtok

strtolower

strtoupper

strtr

substr_compare

substr_count

substr_replace

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

PHP

Home Menu