Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_substr

Returns part of a string.

Available since: PHP 4

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe source string.
$startintPosition of the first character, counting from zero. A negative value counts from the end of the string.
$length?intHow many characters to take. A negative value is how many characters to drop from the end. Defaults to the rest of the string.
$encoding?stringThe encoding. Defaults to the internal one, usually UTF-8.

Return value

stringThe extracted part of the string.

Examples

The first three characters:

<?php
 
echo mb_substr('Программа', 0, 3);

Output:

Про

Negative values count from the end:

<?php
 
echo mb_substr('Программа', -5), "\n";
echo mb_substr('Программа', 0, -1);

Output:

рамма
Программ

Notes

Plain substr() works on bytes and can cut a Cyrillic letter in half. Use the mb_ version for text.

See also

  • mb_strlen — Returns the number of characters in a string.
  • mb_strpos — Finds the position of the first occurrence of a substring.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.