Content · Programming · PHP

PHP mb_strimwidth() Function: Usage and Examples

Learn how to truncate text by display width in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to truncate text by display width. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_strimwidth: Display width is useful for terminals or fixed-width labels; it differs from byte count.

What Is mb_strimwidth in PHP?

Truncate text by display width. Display width is useful for terminals or fixed-width labels; it differs from byte count.

Syntax:

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

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: The trim marker also occupies output width; do not confuse width with character count.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

echo mb_strimwidth("Very long description", 0, 12, "...", "UTF-8");

The code focuses on the main operation. Display width is useful for terminals or fixed-width labels; it differs from byte count.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

echo mb_strimwidth("こんにちは世界", 0, 8, "...", "UTF-8");

What changes? The trim marker also occupies output width; do not confuse width with character count.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$products = ["Compact Keyboard", "Wireless Mouse"];
foreach ($products as $product) {
    echo mb_strimwidth($product, 0, 14, "...", "UTF-8") . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Display width is useful for terminals or fixed-width labels; it differs from byte count.

Important Behavior and Edge Cases

  • Core behavior: Display width is useful for terminals or fixed-width labels; it differs from byte count.
  • Inputs and results: The trim marker also occupies output width; do not confuse width with character count.
  • Alternatives: mb_substr() extracts by character position and mb_strwidth() measures display width.
  • Compatibility: mbstring is required for mb_* functions.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_strimwidthTruncate text by display width.
Related PHP approachmb_substr() extracts by character position and mb_strwidth() measures display width.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionThe trim marker also occupies output width; do not confuse width with character count.
Confusing this API with a related operationmb_substr() extracts by character position and mb_strwidth() measures display width.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions.

Key Takeaways

  • mb_strimwidth helps you truncate text by display width.
  • Display width is useful for terminals or fixed-width labels; it differs from byte count.
  • The trim marker also occupies output width; do not confuse width with character count.

Use mb_strimwidth when you need to truncate text by display width. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.