PHP substr() Function

Usage — The PHP substr() function is used to return part of a string specified by the start and length parameters.

It has the following syntax:

PHP

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

Here is an example of using substr():

PHP

echo substr("Hello world",6);

Return Value — This function returns the extracted part of string; or FALSE on failure, or an empty string. If the start parameter is a negative number and length is less than or equal to start, length becomes 0.

PHP Version and Changelog — The substr() function is available in PHP 4, PHP 5, PHP 7. For version 5.2.2 - 5.2.6, if the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start. In PHP 7.0.0 and later, if string is equal to start characters long, an empty string will be returned. In earlier version, FALSE was returned in this case.

Relevant Functions — Other related PHP functions that you should know about are: strrchr() which finds the last occurrence of a character in a string, substr_replace() which replaces text within a portion of a string, preg_match which performs a regular expression match, trim() which strips whitespace (or other characters) from the beginning and end of a string, mb_substr which gets part of string and wordwrap() which wraps a string to a given number of characters.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

string

The string parameter is used to specify the input string. It must be one character or longer. This is a required parameter.

start

The start parameter is used to specify the start position from which string should be returned. This is a required parameter. If start is non-negative, the returned string will start at the start’th position in string, counting from zero. 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.

length

The length parameter is used to specify the length of the returned string. This is an optional parameter. It default value is to the end of the string. 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.

Working Examples

Here are some examples of using the substr() function:

PHP

// Output — abcde
echo substr("abcdef", 0, -1);

// Output — cde
echo substr("abcdef", 2, -1);

// Output — false
echo substr("abcdef", 4, -4);

// Output — de
echo substr("abcdef", -3, -1);

// Output — f
echo substr("abcdef", -1);

// Output — ef
echo substr("abcdef", -2);

// Output — d
echo substr("abcdef", -3, 1);

// Output — bcdef
echo substr('abcdef', 1);

// Output — bcd
echo substr('abcdef', 1, 3);

// Output — abcd
echo substr('abcdef', 0, 4);

// Output — abcdef
echo substr('abcdef', 0, 8);

// Output — f
echo substr('abcdef', -1, 1);

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. For getting a substring of UTF-8 characters, I highly recommend mb_substr.

    PHP

    $utf8string = "cakeæøå";
    // Output — cake#
    echo substr($utf8string,0,5);
    
    // Output — cakeæ
    echo mb_substr($utf8string,0,5,'UTF-8');
    

    Suggested by - Andreas Bur (andreas dot buro at gmail dot com)

Further Reading

  1. You can read more about the PHP substr() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top