Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony Polyfill Mbstring fails with an undefined iconv_strlen() function

ErrorCall to undefined function Symfony\Polyfill\Mbstring\iconv_strlen()

Short answer

The mbstring polyfill, used when the mbstring extension is missing, calls iconv_strlen() inside its own code. If the server has neither the iconv extension nor symfony/polyfill-iconv, that call fails. Install the mbstring extension for the PHP your web server runs, or enable iconv or add symfony/polyfill-iconv.

  • Applies to symfony/polyfill-mbstring 1.x, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the extensions loaded by the PHP that serves the site. Run it with the same PHP binary or php-fpm version as the site; the command-line PHP can differ from the web one.

    php -m | grep -iE "mbstring|iconv"
    php -v
  2. 2Match what you see, then jump to its fix

    1. 1php -m prints no mbstring lineThe mbstring extension is not installed, so the polyfill runsFix
    2. 2php -m prints no iconv line, and composer show symfony/polyfill-iconv says the package is not installedNeither the iconv extension nor symfony/polyfill-iconv is availableFix

What does the error look like?

  • Call to undefined function Symfony\Polyfill\Mbstring\iconv_strlen()
  • Fatal error: Uncaught Error in vendor/symfony/polyfill-mbstring/Mbstring.php

Cause 1 of 2

The mbstring extension is not installed, so the polyfill runs

1Check

A polyfill is used only when the native function is missing. The polyfill only fills in when the extension is absent, and the iconv_strlen() call comes from inside it.

php -m | grep -i mbstring
composer show symfony/polyfill-mbstring

2Fix

Install the mbstring extension for your PHP version and restart PHP. On Debian or Ubuntu the package is php-mbstring; with a version in the name, such as php8.3-mbstring, use the version you run.

sudo apt install php-mbstring
sudo systemctl restart php8.3-fpm

3Verify

mbstring appears in the module list, then reload the page that failed.

php -m | grep -i mbstring

Confirmed by php.net

Did this fix it?

Cause 2 of 2

Neither the iconv extension nor symfony/polyfill-iconv is available

1Check

The mbstring polyfill calls iconv_strlen() in mb_strlen(), mb_substr() and mb_strwidth(), and the polyfill file does not define that function itself. PHP has the iconv extension on unless it was built with --without-iconv, so a missing one points to a minimal or custom build.

php -m | grep -i iconv
composer show symfony/polyfill-iconv

2Fix

Install the iconv extension for your PHP build, or add the iconv polyfill, which implements the iconv functions in plain PHP.

composer require symfony/polyfill-iconv

3Verify

Check that the function now exists, from the same PHP the site uses.

php -r 'var_dump(function_exists("iconv_strlen"));'

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Install mbstring and iconv in the base image or server setup instead of leaving them to polyfills.
  • Run php -m on the server after each PHP upgrade and compare it with the extensions in composer.json.
  • Check the extension list for php-fpm and for the CLI separately, since they can use different ini files.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.