24

I am currently writing a PHP function but when the complete script is executed I am getting an error:

Error:

Call to undefined function mb_convert_encoding() 

My function:

function cleanData(&$str)
  {
   if($str == 't') $str = 'TRUE';
   if($str == 'f') $str = 'FALSE';
   if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
   $str = "'$str";
  }
  if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  $str = mb_convert_encoding($str, 'ISO-8859-1','utf-8');
}

Can anyone see where I am going wrong. Many thanks in advance for your time.

Cheers

DCJones
  • 3,121
  • 5
  • 31
  • 53
  • 2
    From the [documentation](http://php.net/manual/en/mbstring.installation.php): mbstring is a non-default extension. This means it is not enabled by default. You must explicitly enable the module with the configure option. – Barmar May 25 '16 at 15:14
  • http://stackoverflow.com/a/32987316/524743 – Samuel May 25 '16 at 15:26

4 Answers4

48

You need to install the extension. It depends on of your operating system, here are some examples:

sudo apt-get install php-mbstring  # Debian, Ubuntu
sudo yum install php-mbstring  # RedHat, Fedora, CentOS
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Jorge Barata
  • 2,227
  • 1
  • 20
  • 26
5

On Windows, uncomment the following line in php.ini, and restart the Apache server:

extension=mbstring

If you still get the error afterwards, make sure that what you see is not cached response.

user
  • 23,260
  • 9
  • 113
  • 101
  • php_mbstring.dll is also needed https://stackoverflow.com/a/6658975/6500909 if you are using IIS and run php as cgi – MeSo2 Sep 02 '22 at 23:43
1

If you are using a PHP version other than the latest, you have to specify the version with the extensions you want to download.

Example, in Ubuntu 24.04, I needed to use PHP 7.4. After getting all that set up, to install mbstring, I had to enter:

sudo apt install php7.4-mbstring

Unlike the other examples where you simply enter php-mbstring.

Richard
  • 1,912
  • 20
  • 28
0

function mb_convert_encoding() comes with the php-mbstring package, its not installed or activated by default so you have to enable and activate the extension in php.ini

this example is given by using Ubuntu installed with php 7.2 and apache2

nano /etc/php/7.2/apache2/php.ini

now press ctrl+W and serch for ;mbstring remove the ; symbol press ctrl+S and ctrl+X by order to save and exit

now install the extension

sudo apt-get install php-mbstring

press y in the prompt,

and you also need to restart the apache2

sudo service apache2 restart

and you are ready to use the function.

Aylian Craspa
  • 422
  • 5
  • 11