ServerAvatar Logo

How to Use php substr Function Easily

  • Author: Suresh Ramani
  • Published: 22 July 2025
  • Last Updated: 22 July 2025
How to Use php substr Function Easily

Table Of Contents

Ever tried cutting a slice of cake? That’s exactly what the php substr function does – but for strings! Whether you’re new to PHP or brushing up on the basics, learning how to work with string functions like substr is a must.

In this guide, I’ll walk you through everything you need to know about the php substr function in a friendly, easy-to-understand way. And yes, there will be plenty of examples!

What is php substr?

Think of php substr as a string cutter. Just like you’d slice a piece of bread from a loaf, php substr slices part of a string from a longer one. It’s a built-in PHP function that returns a portion of a string based on the start position and length you provide.

Why Use php substr?

You’d use php substr when you want to:

  • Extract parts of a string (like a domain name from a URL)
  • Trim down user input
  • Show only a preview of a longer text
  • Format strings for better readability

If you’re working on a project that relies on specific PHP behavior, using the correct PHP version is essential. The php substr function behaves consistently, but subtle differences may appear in older versions. Not sure which PHP version to choose? Here’s a detailed guide to help you pick the right PHP version for your server.

Basic Syntax of php substr

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

Let’s break that down:

  • $string – The input string.
  • $start – Position to start slicing.
  • $length (optional) – How many characters to return.

Understanding the Parameters

The real magic happens when you tweak the start and length:

  • Positive start: Counts from the beginning (0 is the first character).
  • Negative start: Starts counting from the end of the string.
  • Omitted length: It slices from the start till the end.

Simple Examples to Start With

PHP
echo substr("Hello World", 0, 5); // Output: Hello

Here, you’re saying: “Give me 5 characters starting from position 0.”

PHP
echo substr("Hello World", 6); // Output: World

In this one, since length is not given, it takes everything after position 6.

Using Negative Start Values

Let’s say you want the last few characters. Negative values come in handy!

PHP
echo substr("Hello World", -5); // Output: World

That’s like saying: “Start 5 characters from the end.”

What Happens If You Omit Length?

If you skip the length parameter, PHP assumes you want everything till the end of the string from your start point.

PHP
echo substr("PHP is fun", 4); // Output: is fun

Sometimes that’s exactly what you want!

Combining substr with Other Functions

You can use php substr with functions like strpos for smarter results.

PHP
$email = "john@example.com";
$atPos = strpos($email, "@");
$username = substr($email, 0, $atPos);
echo $username; // Output: john

This combo is perfect for tasks like extracting usernames from emails.

Common Mistakes and How to Avoid Them

  • Out of range start value: If you go too far, PHP returns false.
  • Passing non-integer start/length: Always use integers.
  • Not checking for false return: Always validate the result before using.
PHP
$result = substr("Hello", 20);
if ($result === false) {
    echo "Start position out of range!";
}

Real-World Use Cases

  • Cutting excerpts from blog posts
  • Masking credit card numbers
  • Displaying the last 4 digits of a phone number
  • Shortening long titles or names
PHP
$cc = "1234567812345678";
echo "**** **** **** " . substr($cc, -4); // Output: **** **** **** 5678

php substr vs. mb_substr

For multilingual strings or Unicode characters, always use mb_substr.

PHP
echo substr("你好世界", 0, 2); // Might break
echo mb_substr("你好世界", 0, 2); // Correct

If you’re dealing with non-English text, mb_substr is the way to go.

Performance Tips

  • Use substr on shorter strings for better performance.
  • Avoid excessive chaining in loops.
  • Cache results if reused multiple times.

These tiny changes can make your scripts faster and more efficient.

When Not to Use php substr

Avoid using php substr:

  • When working with multibyte characters without mb_* support.
  • On binary data unless you’re sure how it works.
  • When you’re parsing structured data like JSON or XML (use appropriate parsers instead).

Useful Tips & Tricks

  • Getting first letter:
PHP
$letter = substr("PHP", 0, 1); // Output: P
  • Removing the last character:
PHP
$trimmed = substr("Hello!", 0, -1); // Output: Hello
  • Loop through a string:
PHP
for ($i = 0; $i < strlen($str); $i++) {
    echo substr($str, $i, 1);
}

Conclusion

There you have it – your quick and easy guide to mastering php substr. It’s a small but powerful function that helps you chop, slice, and manipulate strings like a pro.

Think of php substr as your kitchen knife – simple, sharp, and always useful in the right context.

Next time you need just a part of a string, you’ll know exactly what to do!

Frequently Asked Questions

What does php substr do?

php substr returns a portion of a string, starting from a specific position and optionally ending after a certain length.

Can php substr return false?

Yes, if the start index is out of bounds or the string is empty, substr returns false.

What’s the difference between substr and mb_substr?

mb_substr supports multibyte (Unicode) characters, while substr does not. Use mb_substr for non-English strings.

How can I get the last character of a string in PHP?

mb_substr suUse substr($string, -1) to get the last character.

Can I use substr in PHP to mask data?

Absolutely! You can hide parts of credit card numbers, emails, or phone numbers using php substr.

Ready to Transform Your Hosting?

Join 1,000+ developers who’ve simplified server management with ServerAvatar. Get started for free and deploy your first server and application in minutes.
  • No Credit Card Required
  • Free $10 Server Credits
  • Deploy in next 10 minutes!