Substring in PHP with substr() Function

A substring in PHP or any other programming language, is a portion or segment of a string. For example, a substring might be “PHP” or “Welcome” in the string “Welcome to PHP!”.

We often perform substring operation to extract specific parts of a string for processing or display. To perform this operation, PHP provides a substr() function, which is used to cut a part of a string. Let’s understand it with the help of syntax and examples.

PHP substr() Function


The substr() function in PHP is a built-in function that allows you to cut or extract a specific portion of a string. The general syntax to define substr() function in PHP is as:

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

The substr() function takes three parameters named $string, $start, and $length, respectively.

  • $string specifies the original string from which you want to extract a substring. It must be a valid string value.
  • $start defines the offset or starting position of the substring. It’s zero-based, meaning the first character of the string is at position 0. For example, the first character is at the position 0, second at 1, and so on. You can also use negative numbers to start counting from the end of the string.
  • $length is an optional parameter which defines the length of characters to extract from a string. In other words, this parameter defines how many characters are to be extracted. If you omit this parameter, PHP will return the rest of the string from the start position. You can also pass a negative value to exclude that number of characters from the end.

Note: The question mark (?) before a data type (like ?int) indicates that it’s nullable.

Return Value

The substr() function returns a part or segment of a string, called substring. If $start is greater than the string length, the substr() function returns an empty string. If $length is 0, this function also returns an empty string. However, if $length is negative, the function stops that many characters before the end.

Basic Examples of Substring in PHP


Example 1: Let’s write a PHP program in which we will extract a portion from a specified string using substr() function.

<?php
$text = "Hello, World!";
$result = substr($text, 7);
echo $result;
?>
Output:
      World!


Example 2: Let’s write a PHP program in which we will extract a specific length of substring.

<?php
$text = "Programming in PHP";
// Extract 3 characters starting from position 15.
$result = substr($text, 15, 3);
echo $result;
?>
Output:
      PHP

In this example, we have extracted a substring of three characters starting from the position 14.

Example 3: Let’s write a program in PHP to cut a substring from the end. We will use negative number for the start position.

<?php
$text = "PHP Programming";
// Extract 11 characters starting from end.
$result = substr($text, -11);
echo $result;
?>
Output:
      Programming

Example 4: Write a PHP program to remove characters from the end of a string. In this case, we will use a negative length.

<?php
// Original string.
$text = "Programming in PHP";

// Remove the last 4 characters
$result = substr($text, 0, -4);

echo "Original string: $text\n";
echo "After removing last 4 characters: $result";
?>
Output:
      Original string: Programming in PHP
      After removing last 4 characters: Programming in