HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

substr

Description

The substr of String for PHP 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, 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