Table of contents
1.
Introduction
2.
Definition and Usage 
3.
Syntax
4.
Parameters
5.
Return Type
6.
Examples
6.1.
Example 1: Basic usage of substr()
6.2.
Example 2: Using Negative Index
6.3.
Example 3: Omitting the $length parameter
6.4.
Example 4: Using a Negative Length
7.
Practical Applications of substr()
8.
Frequently Asked Questions
8.1.
What happens if I provide a start index greater than the string length?
8.2.
Can I use negative values for both $start and $length?
8.3.
What if the $length parameter exceeds the string length?
9.
Conclusion
Last Updated: Jan 5, 2025
Easy

PHP substr() Function

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The PHP substr() function is a powerful tool used to extract a part of a string based on specified starting and ending points. This function is widely used in PHP programming for tasks like manipulating strings, trimming text, or extracting specific portions of data. It allows developers to create flexible and dynamic string-processing solutions.

PHP substr() Function

In this article, we will cover the syntax, parameters, and return type of the substr() function, along with examples to help you understand its usage better.

Definition and Usage 

The `substr` function in PHP is used to extract a specific portion of a string. It stands for "substring," which means a smaller part of a larger string. This function is particularly useful when you need to work with only a part of a string, like extracting the first few characters, the last few characters, or even a middle section of a string.

Let’s understand this with an example:  

$text = "Hello, World!";
echo substr($text, 0, 5); 
You can also try this code with Online PHP Compiler
Run Code


Output: 

Hello


In this example, `substr` starts at position `0` (the beginning of the string) and extracts `5` characters, giving us `"Hello"`.

Another example:  

$text = "Hello, World!";
echo substr($text, 7);
You can also try this code with Online PHP Compiler
Run Code


Output: 

World!


Here, `substr` starts at position `7` and extracts everything till the end of the string, resulting in `"World!"`.

You can also use negative numbers for the `$start` parameter. 

For example:  

$text = "Hello, World!";
echo substr($text, -6);
You can also try this code with Online PHP Compiler
Run Code


Output: 

World!


In this case, `-6` means the function starts counting from the end of the string, extracting the last `6` characters.

Note: The `substr` function is versatile and can be used in many scenarios, like trimming strings, extracting file extensions, or even creating custom substrings for dynamic content. It’s a fundamental tool for string manipulation in PHP.

Syntax

The syntax of the substr() function in PHP is as follows:

substr(string $string, int $start, int $length = null): string

 

  • $string: The original string from which you want to extract a portion.  
     
  • $start: The position in the string where the extraction should begin. This can be a positive or negative number.
     
  • $length (Optional): The number of characters to extract. If you don’t specify this, `substr` will return everything from the `$start` position to the end of the string.

Parameters

Let’s break down the parameters of the substr() function:

  1. $string (Required):
    • This is the main input string from which you want to extract a part.
       
    • It can be any string in PHP, such as "Hello World", "Coding Ninjas", etc.
       
    • Example: "Hello World"
  2. $start (Required):
    • This is the index (position) from where the extraction will begin.
       
    • The index is zero-based, which means the first character of the string has index 0.
       
    • If the $start value is positive, it starts from the beginning of the string.
       
    • If the $start value is negative, it starts from the end of the string.
       
  3. Example: For the string "Hello", $start = 1 will extract from the letter 'e' onwards. If $start = -1, it will start from the last character 'o'.
     
  4. $length (Optional):
    • This is the number of characters to return from the start index.
       
    • If you do not specify the $length, the function will extract the substring from the start index to the end of the string.
       
    • If $length is negative, it means you want to exclude the specified number of characters from the end of the string.
       
  5. Example: If the string is "Coding Ninjas" and you set $length = 6, the output will be "Coding".

Return Type

The substr() function returns a substring as a string.

  • If the start and length values are valid, it will return the extracted portion of the original string.
     
  • If the start value is out of the range of the string, it will return an empty string.
     
  • If the length is set to a value that exceeds the remaining length of the string from the start position, the function will return the substring till the end of the string.
     

Let’s look at a few examples to clarify how the substr() function works in practice.

Examples

Example 1: Basic usage of substr()

<?php
$string = "Hello World";
$substring = substr($string, 0, 5);
echo $substring; 
?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Hello

Explanation: Here, the string is "Hello World", and we start at index 0 and extract the first 5 characters. The output is "Hello".

Example 2: Using Negative Index

<?php
$string = "Hello World";
$substring = substr($string, -5, 5);
echo $substring; 
?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

World


Explanation: In this example, we use a negative index -5 to start the extraction from the fifth character from the end. The length is set to 5, so the output is "World".

Example 3: Omitting the $length parameter

<?php
$string = "PHP Programming";
$substring = substr($string, 4);
echo $substring; 
?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Programming


Explanation: Since we did not specify a $length, the function extracts from the start index 4 (i.e., the character 'P') till the end of the string, resulting in "Programming".

Example 4: Using a Negative Length

<?php
$string = "Coding Ninjas";
$substring = substr($string, 0, -2);
echo $substring; 
?>
You can also try this code with Online PHP Compiler
Run Code


 Output: 

Coding Ninja


Explanation: Here, we start at index 0 and extract all characters except the last two, as the length is -2. The output is "Coding Ninja".

Practical Applications of substr()

  1. Trimming strings: You can use substr() to trim excess characters from a string.
     
  2. Extracting data: Often, you may need to extract specific parts of a string, such as a specific word or a numeric value, which can be easily done using this function.
     
  3. Handling URLs and paths: If you are working with URLs or file paths, you might want to extract parts of the URL or path, and substr() is helpful here.

Frequently Asked Questions

What happens if I provide a start index greater than the string length?

If the $start index exceeds the string length, substr() will return an empty string.

Can I use negative values for both $start and $length?

Yes, you can use negative values for both parameters. The $start value counts from the end of the string, and $length will exclude that many characters from the end.

What if the $length parameter exceeds the string length?

If the $length exceeds the remaining characters from the start index, substr() will return the substring until the end of the string.

Conclusion

In this article, we discussed the substr() function in PHP. We learned its syntax, parameters, and return type, and examined multiple examples to see how it works in different scenarios. You now have a good understanding of how to extract portions of a string using PHP, which is an essential skill for string manipulation in web development. 

You can also check out our other blogs on Code360.

Live masterclass