Replacement for mb_strimwidth() which is part of php-mbstring package

One of blogs in my WordPress network is using some old, free theme that was using mb_strimwidth() function. This function is part of php-mbstring package which turned out to be disabled by default in my hosting configuration for PHP 7.2 (it is enabled by default for PHP 5.6).

After enabling PHP 7.2 in my hosting configuration it turned out one of my blogs stopped showing content (showing those damn two small smilies instead). After enabling wp_debug mode it turned out that theme was using mb_strimwidth() that was part of disabled php-mbstring package.

The mb_strlen is not part of that package, seems to be part of core PHP and can be used as a workaround.

In my particular case WordPress theme was using following line of code:

echo mb_strimwidth($content, 0, $character_count, $after);

Replacing it with:

echo mb_strlen($content) > $character_count ? substr($content, 0, $character_count).$after : $content;

solved problem in a matter of minutes.

Notice! Lesson learnt from this situation is that you should always use English version of PHP manual because other language versions may simply by outdated.

The initial version of this article was claiming that mb_strimwidth() is deprecated in PHP 7.2 and that this deprecation was a source of described problem. It turned out that mb_strimwidth() is (of course!) not deprecated in PHP 7.2. I only was using an outdated Polish version of manual page, which has no information about this function in PHP 7. The English version of the very same page has all the information needed.

It wasn’t actually my mistake. I have English set everything I can. It was an effect of incorrect Google search, which (even though I have English set as default language in my Chrome) enforced me to see Polish-language results on top of all other only because I’m on Polish-class IP address. Weird.

Leave a Reply