PHPize Online / SQLize Online

Popular

substr

Tags: PHP 5.x PHP 7.x PHP 8.x

substr — Return part of a string.

Description

substr(
    string $string, 
    int $offset, 
    ?int $length = null
): string
Returns the portion of string specified by the offset and length parameters.

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 Values

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