mb_substr
Returns part of a string.
Available since: PHP 4
Syntax
mb_substr(string $string, int $start, ?int $length = null, ?string $encoding = null): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
| $start | int | Position of the first character, counting from zero. A negative value counts from the end of the string. |
| $length | ?int | How many characters to take. A negative value is how many characters to drop from the end. Defaults to the rest of the string. |
| $encoding | ?string | The 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
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.