PHP substr Function | Return sub-string From A String

PHP substr is an inbuilt function in PHP which returns sub-string from a string. It is a very handy function for extracting a part of the string. In this post, we learn about the PHP substr function along with various examples.

Syntax

substr($string, $startIndex, $stringLength);

Parameters

The PHP substr function expects 2 mandatory input parameters. However, you can also pass an optional third input parameter to get the desired value of sub-string from the string.

  • $string: It is a mandatory parameter of type ‘string’. It is the string from which sub-string is to be returned.
  • $startIndex: It is also a mandatory parameter of type ‘int‘. It can have 3 possible values:
    • Positive: It returns the substring starting from the startIndex value passed from the left of the string.
    • Negative: It returns substring starting from the endIndex value from the end of the string.
    • Zero: It returns the string as it is.
  • $stringLength: It is an optional parameter of type ‘int’. It can also have 3 possible values:
    • Positive: If a positive value is given to stringLength then the substring returned will contain at max those many characters only, starting from startIndex of the string.
    • Negative: When passing a negative value to stringLenght, then that many characters will be discarded from the end of the string.
    • Zero: Empty string returns if zero value is given for stringLength.

Return Value

The PHP substr Function returns a substring of type “string”. The length of the substring depends upon the values of parameters passed to the substr function.

Let us go through some examples to demonstrate the use of the PHP substr function.

PHP substr Function Diagram
PHP substr Function

Examples

Example 1: Positive Start Index

Let’s see an example where we pass a positive start index to PHP substr function. For instance, we pass 6 as start index and see the output.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 6);
print_r($newString);

/*
OUTPUT:

see-what-substr()-does.!

*/

Clearly, the sub-string returned starts after the 6th character position of the original string from the left.

Example 2: Negative Start Index

Now we pass -6 to the substr function and see the output.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, -6);
print_r($newString);

/*
OUTPUT:

does.!

*/

It is clear from above example that on passing negative start index to PHP substr function, the substring is returned from the right of the original string . It’s length is equal to the value of negative start index.

Example 3: 0 as a Start Index

As shown below, on passing 0 as start index value the string is returned as it is.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 0);
print_r($newString);

/*
OUTPUT:

Let's-see-what-substr()-does.!

*/

0 is treated as a valid parameter for start index. However, giving no value to start index will throw the following error:

substr expects at least 2 parameters .

Example 4: Positive string length Value

Let us look at some examples where we pass values to the second parameter of the PHP substr function. We begin with passing a positive value to stringLength. However ,it is purely optional to pass stringLength value.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 0, 5);
print_r($newString);

/*
OUTPUT:
Let's
*/

Clearly, the substring contains 5 characters which is equal to the value of stringLength. The substring starts from 0th position since the startIndex value is 0.

Example 5: Negative stringLength Value

We saw above the output returned when giving positive value to stringLength Similarly, we pasa s negative stringLength value to PHP substr function , as shown below.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 0, -10);
print_r($newString);

/*
OUTPUT:
Let's-see-what-subst
*/

Clearly, the PHP substr function omits or discards as many characters from the right of the original string equal to the value of stringLength. Hence, discarding “r()-does.!” from the original string and returning the rest of it.

Example 6: Positive startIndex and stringLength Value

We now pass positive startIndex and stringLength values to PHP substr function. Let us quickly see what the output is.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 6, 4);
print_r($newString);

/*
OUTPUT:

see-

*/

As we can see above the output is “see-” . As startIndex value given is 6 and stringLenght is 4 , so substring starts from after the 7th character of the string and it’s length is 4 characters long, hence, the output.

Example 7: Positive startIndex and Negative stringLength

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string, 6, -7);
echo "<pre>";
print_r($newString);
echo "</pre>";

/*
OUTPUT:
see-what-substr()
*/

Here, the substring starts from the 7th character of the original string and 7 characters are discarded from the right of the string. This is because of the fact that we gave 6 as startIndex value so substring started from the 7th character. Also, it discarded the last 7 characters because of the negative value of the stringLength parameter.

Example 8: Negative Values for startIndex and stringLength

We now pass negative values to startIndex and stringLength and see the output returned by PHP substr function.

<?php

$string = "Let's-see-what-substr()-does.!";

$newString = substr($string,-6, -1);
print_r($newString);

/*
OUTPUT:

does.

*/

So, the output is “does.“. As you have guessed correctly, this is because the string starts from right up to 6 characters as we gave -6 as startIndex value. As we gave -1 value to stringLenght so 1 character is discarded from the right.

Conclusion

We learnt PHP’s inbuilt PHP substr function and also demonstrated it’s usage by giving many examples. You can learn more about it from PHP’s official documentation. Also, do learn about PHP’s array functions and other string functions from Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *