Search code examples
phpubuntumbstring

Cannot enable mb_string in PHP


I need to use mb_strlen to sort an array of words with diacritics, but PHP does not recognize this function, although mb_string is installed.

So, if I say

function sortByStrlen($a, $b)
{
  if (mb_strlen($a, "utf-8") === mb_strlen($b, "utf-8")) {
    return $a > $b;
  } else {
    return mb_strlen($b, "utf-8") - mb_strlen($a, "utf-8");
  }
}

#sort my array
usort($myArray, 'sortByStrlen');

Won't work on my Ubuntu virtual machine that is running on the server, although it was working on my Windows local machine,

I've spent over 2,5 hours searching this issue and I've tried many solutions, both here in StackOverflow and in AskUbuntu.

These solutions did not work for me:

PHP: Call to undefined function mb_strlen() - on custom compiled PHP with mbstring enabled

Fatal error: Call to undefined function mb_strlen()

Call to undefined function yii\helpers\mb_strlen() on PHP in Yii2 Framework

https://askubuntu.com/questions/772397/mbstring-is-missing-for-phpmyadmin-in-ubuntu-16-04

This is just to name a few! I did a deep search, but I did not find what I need. Please, do not mark it as duplicate because I really need help (indeed, this problem is making me tired).

I already installed mb_string through the terminal:

sudo apt-get install php7.3-mbstring

I'm sure the mbstring module is compatible with my PHP version. Then, I uncommented the lines in php.ini to activate the mbstring module

;extension=mbstring

I also uncommented extensions:

extension_dir = "./"

I restarted the server:

 sudo /etc/init.d/apache2 restart

I also see other modules, like mysql, when I use php-m.

When I run phpinfo, I can see that the mbstring is on the list of the modules being loaded.

If I run php -m on the terminal, it will give me a list of modules and mbstring is there.

HOWEVER, if I run:

<?php

var_dump(extension_loaded('mbstring'));

It will ouput:

bool(false)

Yet, after all of this, I restarted Apache and MySQL many times. So I can't see why mbstring is not being enabled!

As explained at the beginning of this post, I need to sort words with diacritics by their length. So, if you have another solution for this, I will accept it.

I'm not getting error messages because this is for production. The code is running on my personal website in a virtual machine; because it is production, PHP does not show errors by default and I don't know how to enable them.

Notes: Ubuntu version is 18.04 PHP version is 7.3

UPDATE 2

As requested by @gview, if I run dpkg -l | grep php, I get:

ii  libapache2-mod-php7.3           7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        server-side, HTML-embedded scripting language (Apache 2 module)
ii  php-common                      2:69+ubuntu18.04.1+deb.sury.org+2+php7.3                 all          Common files for PHP packages
ii  php-gettext                     1.0.12-0.1                                               all          transitional dummy package for php-php-gettext
ii  php-mbstring                    2:7.3+69+ubuntu18.04.1+deb.sury.org+2+php7.3             all          MBSTRING module for PHP [default]
ii  php-pear                        1:1.10.8+submodules+notgz-1+ubuntu18.04.1+deb.sury.org+1 all          PEAR Base System
ii  php-php-gettext                 1.0.12-0.1                                               all          read gettext MO files directly, without requiring anything other than PHP
ii  php-xml                         2:7.3+69+ubuntu18.04.1+deb.sury.org+2+php7.3             all          DOM, SimpleXML, WDDX, XML, and XSL module for PHP [default]
ii  php7.3                          7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     all          server-side, HTML-embedded scripting language (metapackage)
ii  php7.3-cli                      7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        command-line interpreter for the PHP scripting language
ii  php7.3-common                   7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        documentation, examples and common module for PHP
ii  php7.3-json                     7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        JSON module for PHP
ii  php7.3-mbstring                 7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        MBSTRING module for PHP
ii  php7.3-mysql                    7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        MySQL module for PHP
ii  php7.3-opcache                  7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        Zend OpCache module for PHP
ii  php7.3-readline                 7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        readline module for PHP
ii  php7.3-xml                      7.3.7-2+ubuntu18.04.1+deb.sury.org+1                     amd64        DOM, SimpleXML, WDDX, XML, and XSL module for PHP

That may help you help me.


Solution

  • After over 3,5 hours of search, I found the solution!!! I HOPE IT WILL HELP SOMEONE ELSE !!

    THE ERROR

    So, the problem was that the extension_dir in the php.ini file was set to the wrong file. By default, it was to the following:

    extension_dir = './'
    

    THE SOLUTION

    1. Open you terminal ctrl+alt+t and type:
    php -i | grep extension_dir
    

    In my case, it ouputs:

    /user/lib/php/20180731
    
    1. Find out your php.ini file: You can do that either by:
    <?php
    phpinfo();
    

    Or on the terminal

    php -i | grep 'php.ini'
    
    1. Go to your php.ini file and change the following line:
    extension_dir = '/full/path/to/your/php/modules'
    

    in my case, it was:

    exension_dir = '/usr/lib/php/20180731'
    

    SEE MORE

    By the way, you have to do this after installing mbstring. To do that, You may find useful the following links:

    PHP: Call to undefined function mb_strlen() - on custom compiled PHP with mbstring enabled

    Fatal error: Call to undefined function mb_strlen()

    Call to undefined function yii\helpers\mb_strlen() on PHP in Yii2 Framework

    https://askubuntu.com/questions/772397/mbstring-is-missing-for-phpmyadmin-in-ubuntu-16-04