PHP mb_substr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_substr() returns a part of a string, counted in characters, using a character encoding you can choose.

Unlike substr(), it never cuts an accented letter or a symbol in half.

Syntax

Call mb_substr() with the values you need. Start with this line.

PHP
$part = mb_substr($text, 0, 5, "UTF-8");
CallWhat it does
mb_substr("Héllo Wörld", 0, 5, "UTF-8")Returns "Héllo".
mb_substr("Héllo Wörld", -5, null, "UTF-8")Returns "Wörld".

A negative start counts from the end of the string. The first character is at position 0.

Library declarations
PHP
mb_substr(string $string, int $start, ?int $length = null, ?string $encoding = null): string

Parameters

ParameterDescription
stringRequired. The source text.
startRequired. The start position. A negative value counts from the end.
lengthOptional. The number of characters to return. Default is to the end.
encodingOptional. The character encoding. Default is the internal encoding.

Return Value

Return ValueDescription
stringThe part of the string. It is an empty string if start is past the end.

PHP Examples

Example 1 — Take the First Letters

Cut the first five characters of a text with accents.

PHP
<?php
$text = "Héllo Wörld";

echo mb_substr($text, 0, 5, "UTF-8"), PHP_EOL;

How It Works

  1. Start at position 0.
  2. Take 5 characters. The é counts as one.
  3. No letter is cut in half.
  4. Print the part. You see Héllo.

Example 2 — Count from the End

A negative start counts from the end.

PHP
<?php
$text = "Héllo Wörld";

echo mb_substr($text, -5, null, "UTF-8"), PHP_EOL;

How It Works

  1. Use -5. The count starts 5 characters from the end.
  2. Pass null as the length. It means to the end.
  3. You get the last five characters.
  4. The ö is kept whole.

Example 3 — Cut Japanese Text

Pick two characters from the middle of a Japanese sentence.

PHP
<?php
$text = "こんにちは世界";

echo mb_substr($text, 5, 2, "UTF-8"), PHP_EOL;

How It Works

  1. The text has 7 characters.
  2. Start at position 5. That is the character 世.
  3. Take 2 characters.
  4. You get 世界, which means world.

Frequently Asked Questions

mb_substr() returns part of a string by character position, using a character encoding.
mb_substr(string $string, int $start, ?int $length = null, ?string $encoding = null): string.
substr() cuts by bytes and can break a UTF-8 letter. mb_substr() cuts by characters.
Yes. A negative start counts from the end of the string.
Use mb_substr($text, 0, 10, 'UTF-8') . '...' to cut after 10 characters and add three dots.

Back to mbstring Functions

Browse all PHP mbstring function tutorials.

mbstring functions →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful