PHP 8 reference · Multibyte String Functions

PHP mb_substr() Function

Return part of a string by character position, so multibyte characters are never cut in half. The UTF-8 version of substr().

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
start The first character, from 0; negative counts from the end.
length optional How many characters; null takes the rest.
encoding optional The character encoding; defaults to mb_internal_encoding() (UTF-8).

Return value

The extracted part.

Examples

Take charactersTry it yourself
<?php
$city = "Zürich";
echo mb_substr($city, 0, 2), "\n";
echo mb_substr($city, -3), "\n";

Output

Zü
ich
Shorten text safelyTry it yourself
<?php
$text = "Crème brûlée is a French dessert";
echo mb_strlen($text) > 15 ? mb_substr($text, 0, 15) . "…" : $text, "\n";

Output

Crème brûlée is…

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?