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
phpmb_substr(string, start, [length], [encoding]): string;
Example
Let's extract 3 characters from the string, starting from position 1:
php<?php
$res = mb_substr('абвгд', 1, 3);
echo $res;
?>
Code execution result:
php'бвг'
Example
Let's extract a substring to the end of the string from "日本語", starting from position 1:
php<?php
$res = mb_substr('日本語', 1);
echo $res;
?>
Code execution result:
php'本語'
Example
Let's extract a substring specifying the UTF-8 encoding:
php<?php
$res = mb_substr('абвгд', 2, 2, 'UTF-8');
echo $res;
?>
Code execution result:
php'вг'