call to undefined function mb_strimwidth

call to undefined function mb_strimwidth

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Mystery Behind mb_strimwidth() and How to Restore It

Running a script that once worked perfectly, only to be met with a Fatal error: Call to undefined function mb_strimwidth(), is a frustrating experience for any PHP developer. You locate the mbstring extension in your installation, confirm the presence of the function within its documentation or source headers, and then hit a wall when you discover the function simply does not exist at runtime. The immediate instinct is often to assume the extension isn't loaded, but checking phpinfo() usually reveals that mbstring is indeed active.

The root cause here lies in how PHP manages extensions and their specific functions. In many modern distributions of PHP (such as those running on Ubuntu or via Docker containers), the mbstring extension is compiled without certain legacy or less frequently used functions like mb_strimwidth(). While the core multibyte string functionality remains intact, this specific function was effectively deprecated in favor of more flexible alternatives or stripped out to reduce binary size. This isn't a bug in your code; it is a configuration decision made during the build process of the PHP package itself.

To replicate the behavior of mb_strimwidth()—which truncates a string to a specified width and replaces the overflowing characters with ellipsis—you need to construct a manual solution using available tools. The most robust approach involves combining mb_substr(), mb_strlen(), and a simple conditional check.

Here is a function that mimics the original behavior:

function strimwidth_with_ellipsis(string $str, int $width, string $ellipsis = '...'): ?string {
    // If the string fits within the width, return it unchanged.
    if (mb_strlen($str, 'UTF-8') <= $width) {
        return $str;
    }

    // Determine the maximum number of characters we can keep.
    // We subtract 1 to account for the ellipsis itself occupying space.
    $maxChars = $width - mb_strlen($ellipsis, 'UTF-8');

    if ($maxChars <= 0) {
        return $ellipsis;
    }

    // Truncate the string and append the ellipsis.
    $truncated = mb_substr($str, 0, $maxChars, 'UTF-8') . $ellipsis;

    return $truncated;
}

This implementation handles the critical edge case where the ellipsis itself might exceed the requested width, preventing the function from returning a string longer than the limit. It also respects the UTF-8 encoding context, which is essential for handling emojis or CJK characters where a single glyph might take up multiple bytes but should only count as one character in terms of visual width.

One significant trade-off to consider is that this custom function does not account for the specific "width" logic that mb_strimwidth() attempted to handle regarding variable-width characters in some legacy implementations. However, in modern web development, relying on byte length or standard Unicode code point counts via mb_strlen is generally sufficient for UI truncation purposes.

If you are building a Laravel application and need to ensure this functionality is consistently available across your stack without maintaining custom utility functions everywhere, consider creating a service provider trait or placing these helpers in a shared namespace. Alternatively, if the function is required for compatibility with older libraries that strictly depend on mb_strimwidth(), you may need to patch your PHP build configuration or utilize an alternative extension like intl if available, though custom wrappers usually offer better control over edge cases.

For instance, if you are using a specific hosting provider where the default PHP installation strips these functions, verifying the php.ini settings for enabled extensions is a necessary first step. However, relying on a fallback implementation like the one above ensures your table cell formatting logic remains robust regardless of the underlying PHP build quirks.

Our network