mb_strimwidth()

mb_strimwidth() — Get truncated string with specified width.

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

Truncates string string to specified width, where halfwidth characters count as 1, and fullwidth characters count as 2. See https://www.unicode.org/reports/tr11/ for details regarding East Asian character widths.

Signature

mb_strimwidth(string $string, int $start, int $width, string $trim_marker = "", ?string $encoding = null): string

Parameters

ParameterTypeWhat it takes
$stringstringThe string being decoded.
$startintThe start position offset. Number of characters from the beginning of string (first character is 0), or if start is negative, number of characters from the end of the string.
$widthintThe width of the desired trim. If negative width is specified, count from the end of the string. Note: Passing negative width is deprecated as of PHP 8.3.0.
$trim_markerstringA string that is added to the end of string when string is truncated.
$encoding?stringThe encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return value

The truncated string. If trim_marker is set, trim_marker replaces the last chars to match the width.

Example

A runnable snippet:

echo mb_strimwidth("Hello World", 0, 10, "..."); // output: "Hello W..."

Changelog

VersionChange
8.3.0Passing a negative width to mb_strimwidth() is now deprecated.
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.
7.1.0Support for negative starts and widths has been added.

See also

mb_strwidth(), mb_internal_encoding()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mb_strimwidth() (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.