"Mastering PHP: substr Function Examples for String Manipulation"

Sandra Jun 29, 2026

Mastering PHP substr: Extracting String Segments with Ease

Sessions In PHP
Sessions In PHP

In the vast landscape of web development, PHP is a language that stands out due to its simplicity and power. One of its many useful features is the `substr` function, which allows you to extract substrings from a given string. Let's dive into the world of PHP `substr` with practical examples and tips to enhance your coding skills.

the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber

Understanding PHP substr Syntax

Before we delve into examples, let's understand the basic syntax of the `substr` function:

PHP Basic Syntax and Comments Overview (With Examples)
PHP Basic Syntax and Comments Overview (With Examples)

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

  • string: The string from which you want to extract a substring.
  • start: The position at which to start extracting the substring. Indexing starts from 0.
  • length (optional): The number of characters to extract. If not provided, PHP will extract the remaining characters from the starting position to the end of the string.
a screen shot of a web page with the words'html input types '
a screen shot of a web page with the words'html input types '

Basic PHP substr Example

Let's start with a simple example. Suppose we have a string `message` and we want to extract the first 5 characters:

```php $message = "Hello, World!"; $substring = substr($message, 0, 5); echo $substring; // Outputs: Hello ```

Starting from the Middle

PHPSpreadsheet Quickstart Tutorial
PHPSpreadsheet Quickstart Tutorial

You can also start extracting from any position in the string, not just the beginning:

```php $substring = substr($message, 7, 5); echo $substring; // Outputs: World ```

Extracting the Rest of the String

If you omit the third parameter, `substr` will extract the remaining characters from the starting position:

Types of Operators In PHP (With Examples)
Types of Operators In PHP (With Examples)

```php $substring = substr($message, 7); echo $substring; // Outputs: World! ```

Negative Indexing in PHP substr

PHP `substr` also supports negative indexing, which can be quite useful:

PHP Syntax for beginners Cheat Sheet
PHP Syntax for beginners Cheat Sheet
the different types of text and numbers are shown in this graphic diagram, which shows how to
the different types of text and numbers are shown in this graphic diagram, which shows how to
Subscript and Superscript in HTML!
Subscript and Superscript in HTML!
Display PHP Variables In HTML
Display PHP Variables In HTML
an image of a computer screen with text
an image of a computer screen with text
Simple Edit Data Using PHP and MYSQL [Responsive Page]
Simple Edit Data Using PHP and MYSQL [Responsive Page]
an image of a web page with the text'html inputt types '
an image of a web page with the text'html inputt types '
Advance Pagination with jQuery and PHP
Advance Pagination with jQuery and PHP
CSRF Token in PHP (Very Simple Example)
CSRF Token in PHP (Very Simple Example)
an image of a web page with the text'how to write a html list '
an image of a web page with the text'how to write a html list '
html and css login form
html and css login form
MySQL Select Statement using PHP with Source Code
MySQL Select Statement using PHP with Source Code
an image of a web page with different types in the form of words and numbers
an image of a web page with different types in the form of words and numbers
A Look Into Proper HTML5 Semantics
A Look Into Proper HTML5 Semantics
Insert and Display Data in PHP & MYSQL Without Refreshening Page
Insert and Display Data in PHP & MYSQL Without Refreshening Page
CSS Introduction (What is CSS) | Beginner Guide to Cascading Style Sheets
CSS Introduction (What is CSS) | Beginner Guide to Cascading Style Sheets
Differences between JavaScript and PHP
Differences between JavaScript and PHP
All HTML Input Types 📌 Save it or Miss it 🚀 Follow For More 💥
All HTML Input Types 📌 Save it or Miss it 🚀 Follow For More 💥
Pardon bâtir un index de schéma réactant en HTML CSS et JavaScript |  SwiperJs
Pardon bâtir un index de schéma réactant en HTML CSS et JavaScript | SwiperJs
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
  • substr($string, -1): Extracts the last character.
  • substr($string, -5): Extracts the last 5 characters.
  • substr($string, -5, 3): Extracts the last 3 characters.

Comparing substr with Other String Functions

PHP provides several string functions that can be used in conjunction with `substr` or as alternatives. Here's a comparison:

Function Purpose Example
substr Extracts a substring based on start position and length. substr("Hello, World!", 7, 5) returns "World".
strlen Returns the length of a string. strlen("Hello, World!") returns 13.
strpos Returns the position of the first occurrence of a substring. strpos("Hello, World!", "World") returns 7.

Practical Use Cases of PHP substr

Here are a few practical use cases of `substr` in PHP:

  • Extracting a username from an email address:

```php $email = "john.doe@example.com"; $username = substr($email, 0, strpos($email, '@')); echo $username; // Outputs: john.doe ```

  • Limiting the length of a string:

```php $text = "This is a very long text that needs to be truncated."; $truncatedText = substr($text, 0, 50) . "..."; echo $truncatedText; // Outputs: This is a very long text that needs to be truncated... ```

In conclusion, `substr` is a powerful function that can help you manipulate strings in PHP with ease. Whether you're extracting a substring, limiting the length of a string, or performing other string operations, `substr` is a versatile tool to have in your PHP toolbox.