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:
- $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"
- $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.
- 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'.
- $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.
- 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()
- Trimming strings: You can use substr() to trim excess characters from a string.
- 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.
- 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.