The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally a length. It cuts the string based on those numbers and gives you the piece you asked for.
Table of Content
Understand the substr Function in PHP
You use substr()
when you need to pull out a part of a string. Maybe you want just the first few letters. Maybe you need everything after a certain point. It works well when you know the position of the part you want.
Here is the full syntax for substr()
:
substr(string $string, int $start, ?int $length = null): string
Here, what each part does.
$string
This is the full string you want to work with. It can be any string.$start
This tells PHP where to start cutting. The count starts from 0. If you use a negative number, PHP counts from the end of the string.$length
(optional)
This tells PHP how many characters to return. If you leave it out, PHP gives you everything from the start point to the end of the string. If the number is negative, PHP will stop that many characters from the end.
A Quick example:
$text = "Welcome to flatcoding tutorials";
echo substr($text, 0, 7);
Output:
Welcome
PHP starts at index 0 and returns the next 7 characters.
You can also use a negative start value:
echo substr($text, -20);
This starts 20 characters from the end and goes to the end of the string. Its output:
flatcoding tutorials
Examples of the substr Function in PHP
Multibyte strings:
If you work with non-English characters like Japanese, Arabic, or emojis, substr()
might not work correctly. That is because substr()
counts bytes, not full characters.
$text = "こんにちは"; // Japanese
echo substr($text, 0, 3); // Broken output: こ
This function works on standard strings. For multibyte support, see other PHP functions.
Mix substr()
with another string:
You can mix substr()
with other string functions. Here is an example:
$email = "[email protected]";
$domain = substr(strrchr($email, "@"), 1);
echo $domain;
The output:
example.com
This first finds the @
and then cuts the domain part.
Extracting file extensions:
You can get a file extension like this:
$file = "photo.jpg";
$ext = substr($file, strrpos($file, '.') + 1);
echo $ext;
The output:
jpg
Wrapping Up
substr()
gives you control over string parts based on position and length. It is simple, fast, and useful for many cases.
Here is a quick recap:
substr()
returns part of a string with a start position and optional length.- Use positive or negative numbers to control where it starts and how much it returns.
- If you leave out the length, PHP gives everything from the start to the end.
substr()
works well to truncate text and slice values. Also to clean user input.- If the start index is too big, PHP returns an empty string.
- If the length is too large, PHP stops at the end of the string.
- Don’t use
substr()
inside loops unless needed.
Thank you for reading. Click here to see more PHP tutorials.
FAQ’s
How does substr() work in PHP?
How to get a file extension using substr()?
Similar Reads
The array_pop function in PHP. Such a small function, yet so handy when you have to work with the last…
The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…
You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…
The increment and decrement are operators that can be used to increase or decrease the variable values which have a…
If you start working with PHP, it won't take you that long to figure out that strings are everywhere. Besides…
PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…
If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…
If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…
PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…
If you find a word that does not fit and want to fix it. You can use the PHP str_replace…