Mastering PHP: substr Function – Extract Strings with Ease

Sandra Jun 29, 2026

Mastering PHP substr Function: Extracting and Manipulating Strings

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

In the vast landscape of PHP, the substr function stands as a powerful tool for string manipulation. It allows developers to extract substrings from larger strings, a task that is often crucial in web development. This article aims to provide a comprehensive guide to the PHP substr function, ensuring you understand its syntax, usage, and some practical applications.

Setting and Destroying SESSION in PHP
Setting and Destroying SESSION in PHP

Understanding the substr Function Syntax

The PHP substr function follows a straightforward syntax:

PHPSpreadsheet Quickstart Tutorial
PHPSpreadsheet Quickstart Tutorial

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. If it's negative, it starts from the end of the string.
  • $length: The number of characters to extract. If not specified, it extracts the remaining characters from the start position to the end of the string.

Extracting Substrings with substr

Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions
Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions

Let's start with a simple example. Suppose we have a string:

$str = "Hello, World!";

To extract the substring "World", we can use:

$substring = substr($str, 7);

The output will be: World!

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

Specifying Length

You can also specify the length of the substring to extract. For instance, to extract only "World" without the exclamation mark:

$substring = substr($str, 7, 5);

The output will be: World

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

Starting from the End

substr can also start from the end of the string. To extract the last 5 characters of the string:

an image of a computer screen with the text subgreies - the hidden super power in
an image of a computer screen with the text subgreies - the hidden super power in
an important python functions chart with the words and numbers on it, including instructions to use them
an important python functions chart with the words and numbers on it, including instructions to use them
Subscript and Superscript in HTML!
Subscript and Superscript in HTML!
PHP Output Buffering
PHP Output Buffering
10 PHP Functions That Will Speed Up Your Coding Efficiency
10 PHP Functions That Will Speed Up Your Coding Efficiency
an image of a computer screen with the words, understanding php syntax
an image of a computer screen with the words, understanding php syntax
Python Next Function (With Examples)
Python Next Function (With Examples)
a blackboard with instructions on how to use the functions for an appliance
a blackboard with instructions on how to use the functions for an appliance
All HTML Input Types 📌 Save it or Miss it 🚀 Follow For More 💥
All HTML Input Types 📌 Save it or Miss it 🚀 Follow For More 💥
Student Information Management System in PHP
Student Information Management System in PHP
PHP Security Best Practices 2025 Guide
PHP Security Best Practices 2025 Guide
Differences between JavaScript and PHP
Differences between JavaScript and PHP
PHP Control Statement Types
PHP Control Statement Types
the inputt function in python is a built - in function that allows users to compute data from the command line or terminal
the inputt function in python is a built - in function that allows users to compute data from the command line or terminal
Python Remove Substring with Examples
Python Remove Substring with Examples
PHP Project | Image Upload System (Part 01)
PHP Project | Image Upload System (Part 01)
a blue background with text that says,'user = $ get sets > get user
a blue background with text that says,'user = $ get sets > get user
a blackboard with instructions on how to use functions in python
a blackboard with instructions on how to use functions in python
GET vs POST vs PUT vs DELETE — HTTP Methods in Express.js Explained
GET vs POST vs PUT vs DELETE — HTTP Methods in Express.js Explained
MySQL Select Statement using PHP with Source Code
MySQL Select Statement using PHP with Source Code

$substring = substr($str, -5);

The output will be: !World

Using substr with Multibyte Strings

PHP supports multibyte strings, and substr works perfectly with them. However, you should be aware of the encoding of your strings. If your string is encoded in UTF-8, for example, you might need to use mb_substr instead, which is specifically designed for multibyte strings.

Practical Applications of substr

Truncating Text

substr is often used to truncate long texts. For instance, you might want to display only the first 100 characters of a blog post on your homepage:

$truncated_post = substr($post->content, 0, 100);

Extracting Data from Strings

substr can also be used to extract data from strings. For example, if you have a string containing an email address, you can extract the domain:

$domain = substr($email, strpos($email, '@') + 1);

substr vs. substr_replace: When to Use Each

PHP also provides substr_replace, which replaces a substring with another string. The choice between substr and substr_replace depends on your needs. If you only need to extract a substring, use substr. If you need to replace a substring, use substr_replace.

Function Purpose
substr Extracts a substring from a string
substr_replace Replaces a substring with another string

In conclusion, the PHP substr function is a versatile tool for string manipulation. Whether you're extracting data, truncating text, or working with multibyte strings, substr has you covered. Mastering this function will greatly enhance your PHP skills and make your coding life easier.