PHP substr Function

The substr function returns a portion of the string you pass to it. Starting location and length parameters determine which portion of that string to return. The following example demonstrates using substr to return the first five characters in the string:

$str = 'Hello World';
// arguments: string, start location, length
$part = substr($str, 0, 5);
var_dump($part); // string(5) "Hello"

substr Arguments and Options

The first argument to substr is the string you wish to select a substring from. The second argument is a zero-based index location in the string at which to start the substring selection. The third argument, length, is optional. It specifies the number of characters to be included in the substring returned.

If no length argument is included, the portion of the string from the start location through the end of the string is returned, as we show here:

$str = 'Hello World';
// return part of $str from location 6 (7th character) to end
$part = substr($str, 6); 
var_dump($part); // string(5) "World"

Both start and length arguments to the substr function can be either positive or negative integers.

Negative Start Location Argument

If a negative number is used to specify the start location, then the start location is that number of characters from the end of the string. So if the start location is -5 and no length argument is provided, the last five characters of the string are returned:

$str = 'Hello World';
$part = substr($str, -5); 
var_dump($part); // string(5) "World"

Our next example demonstrates a negative integer for the start argument with a positive integer for length:

$str = 'Hello World';
// return 2 characters starting 5 from end of $str
$part = substr($str, -5, 2);
var_dump($part); // string(2) "Wo"

Negative Length Argument

A negative third argument specifies the number of characters to omit from the end in the string returned by substr. The following two examples demonstrate negative length arguments.

$str = 'Hello World';
$part = substr($str, 6, -3);
var_dump($part); // string(2) "Wo"

For the example above, with the start location 6 and the length argument -3, the returned substring consists of character 7 up to but not including the third character from the end.

For the example below, with the start location -4 and the length argument -2, the returned substring consists of the characters starting four from the end up to but not including the second character from the end.

$str = 'Hello World';
$part = substr($str, -4, -2);
var_dump($part); // string(2) "or"

substr with strpos or strrpos

The substr function is often used in conjunction with strpos or strrpos functions which find the location of a particular character or substring in a string. We use substr with strpos to return the first word in the string in the following example:

$str = 'Hello World';
// return from beginning of string to first space
$part = substr($str, 0, strpos($str, ' ') );
var_dump($part); // string(5) "Hello"

The next example uses substr with strrpos to find the last space character in the string and return the substring from that character location plus one to the end of the string, i.e., to return the last word in the string:

$str = 'Hello World';
// 
$part = substr($str, strrpos($str, ' ') + 1 );
var_dump($part); // string(5) "World"

Find out more about the strpos and strrpos functions and their case insensitive versions: stripos and strripos in our discussion on searching strings in PHP.

substr_compare and substr_replace

PHP provides two functions related to substr that are used to compare and replace substrings: substr_compare and substr_replace. We describe and demonstrate them at Comparing Strings in PHP and Replacing Strings in PHP.

Back to top