Return part of a string


substr ( string $string , int $start [, int $length ] ) : string



The input string.


If start is non-negative, the returned string will start at the start'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 start is negative, the returned string will start at the start'th character from the end of string. If string is less than start characters long, FALSE will be returned.


If length is given and is positive, the string returned will contain at most length characters beginning from start (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 start is negative). If start 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 start until the end of the string will be returned.


Returns the extracted part of string; or FALSE on failure, or an empty string.


string start | Negative


$string = "abcde";
$start1 = -5;
$start2 = -1;

for ($i = $start1; $i <= $start2; ++$i) {
    echo "start:$i return:" . substr($string, $i) . PHP_EOL;

start:-5 return:abcde
start:-4 return:bcde
start:-3 return:cde
start:-2 return:de
start:-1 return:e

string start | Non-negative


$string = "abcde";
$start1 = 0;
$start2 = 4;

for ($i = $start1; $i <= $start2; ++$i) {
    echo "start:$i return:" . substr($string, $i) . PHP_EOL;

start:0 return:abcde
start:1 return:bcde
start:2 return:cde
start:3 return:de
start:4 return:e

length | Negative


$string = "abcde";
$start1 = -5;
$start2 = 4;
$length1 = -5;
$length2 = -1;

for ($i = $start1; $i <= $start2; ++$i) {
    for ($j = $length1; $j <= $length2; ++$j) {
        echo "start:$i length:$j return:" . substr($string, $i, $j) . PHP_EOL;

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

length | Non-negative


$string = "abcde";
$start1 = -5;
$start2 = 4;
$length1 = 0;
$length2 = 4;

for ($i = $start1; $i <= $start2; ++$i) {
    for ($j = $length1; $j <= $length2; ++$j) {
        echo "start:$i length:$j return:" . substr($string, $i, $j) . PHP_EOL;

start:-5 length:0 return:
start:-5 length:1 return:a
start:-5 length:2 return:ab
start:-5 length:3 return:abc
start:-5 length:4 return:abcd
start:-4 length:0 return:
start:-4 length:1 return:b
start:-4 length:2 return:bc
start:-4 length:3 return:bcd
start:-4 length:4 return:bcde
start:-3 length:0 return:
start:-3 length:1 return:c
start:-3 length:2 return:cd
start:-3 length:3 return:cde
start:-3 length:4 return:cde
start:-2 length:0 return:
start:-2 length:1 return:d
start:-2 length:2 return:de
start:-2 length:3 return:de
start:-2 length:4 return:de
start:-1 length:0 return:
start:-1 length:1 return:e
start:-1 length:2 return:e
start:-1 length:3 return:e
start:-1 length:4 return:e
start:0 length:0 return:
start:0 length:1 return:a
start:0 length:2 return:ab
start:0 length:3 return:abc
start:0 length:4 return:abcd
start:1 length:0 return:
start:1 length:1 return:b
start:1 length:2 return:bc
start:1 length:3 return:bcd
start:1 length:4 return:bcde
start:2 length:0 return:
start:2 length:1 return:c
start:2 length:2 return:cd
start:2 length:3 return:cde
start:2 length:4 return:cde
start:3 length:0 return:
start:3 length:1 return:d
start:3 length:2 return:de
start:3 length:3 return:de
start:3 length:4 return:de
start:4 length:0 return:
start:4 length:1 return:e
start:4 length:2 return:e
start:4 length:3 return:e
start:4 length:4 return:e


Home Menu