mb_substr()

mb_substr() — Get part of string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Performs a multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of string. First character's position is 0. Second character position is 1, and so on.

Signature

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

Parameters

ParameterTypeWhat it takes
$stringstringThe string to extract the substring from.
$startintIf start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
$length?intMaximum number of characters to use from string. If omitted or null is passed, extract all characters to the end of the string.
$encoding?stringThe encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return value

mb_substr() returns the portion of string specified by the start and length parameters.

Example

The PHP manual ships no example for mb_substr(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.3.2On invalid strings (those with encoding errors), character indices are now interpreted in the same manner as most other mbstring functions. This means that character indices returned by mb_strpos() can be passed directly. For SJIS-mac (MacJapanese) strings, character indices now refer to the ...
8.0.0encoding is nullable now.
8.0.0Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned.

See also

mb_strcut(), mb_internal_encoding()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mb_substr() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.