The mb_substr Function
The mb_substr function extracts and returns a substring from a string, correctly working with multibyte encodings (e.g., UTF-8). The first parameter is the string, the second is the starting position, the third (optional) is the substring length. The fourth parameter can specify the encoding.
Syntax
mb_substr(string, start, [length], [encoding]): string;
Example
Let's extract 3 characters from the string, starting from position 1:
<?php
$res = mb_substr('абвгд', 1, 3);
echo $res;
?>
Code execution result:
'бвг'
Example
Let's extract a substring to the end of the string from "日本語", starting from position 1:
<?php
$res = mb_substr('日本語', 1);
echo $res;
?>
Code execution result:
'本語'
Example
Let's extract a substring specifying the UTF-8 encoding:
<?php
$res = mb_substr('абвгд', 2, 2, 'UTF-8');
echo $res;
?>
Code execution result:
'вг'