65

I recently switched to PHP 7 on my development server, which has worked just fine - until now.

Since I updated to PHP 7.0.3-10+deb.sury.org~trusty+1 (earlier today), the utf8_decode and utf8_encode functions are no longer accessible. They were, however, in previous versions of PHP7. When called, a fatal error is raised.

I read that these functions are provided by the mbstring extension, which I checked with var_dump(extension_loaded('mbstring')); is loaded.

How can I get the above functions to work again?

paolo
  • 2,304
  • 3
  • 13
  • 25
  • 1
    `mbstring` does not provide this function. Oddly enough, it's listed in [XML Parser Functions](http://php.net/manual/en/ref.xml.php). Maybe you'd like to elaborate a bit on the error message? Quick testing on my (custom) PHP7 shows it "still" works... – Linus Kleen Feb 29 '16 at 14:23
  • The full error message is `Fatal error: Uncaught Error: Call to undefined function utf8_encode() in ...`. I managed to replace the encode function with `mb_convert_encoding($value, 'UTF-8')`, but I still want to know what causes the error. – paolo Feb 29 '16 at 14:40
  • I needed the function to convert strings I got from LDAP. What else can I tell about the error message? – paolo Feb 29 '16 at 14:41
  • Is this the *same* package? That is, did 7.0.3-9 not produce this error? If so, you might want to consider filing a bug report with `deb.sury.org`. – Linus Kleen Feb 29 '16 at 14:52
  • Unfortunately, I don't remember which version the server ran before, but the error did definitely not occur before the update to 7.0.3-10. I will do some research about the recent changes of PHP7. Thanks for bringing me on track! – paolo Feb 29 '16 at 14:58
  • If you run `php -m` does it list `xml`? You probably need to install some package like `php7-xml`. – NikiC Feb 29 '16 at 15:21
  • Nailed it, thank you! No `xml` extension -> installed it -> problem solved. Feel free to write this as an answer so I can officially accept it. – paolo Feb 29 '16 at 15:29
  • `mb_convert_encoding($value, 'UTF-8')` only does the same thing as `utf8_encode($value)` if your `default_charset` (PHP 5.6+) or `mbstring.internal_encoding` (PHP 5.5 or earlier) is set to "ISO-8859-1". Otherwise you'll want to explicitly set the from encoding with `mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1')` – Matt Raines Feb 29 '16 at 16:13
  • Thanks for the hint. I'm back to `utf8_encode` anyways. – paolo Feb 29 '16 at 16:19

7 Answers7

186

I had the same problem. Just install php7.0-xml package. Ubuntu 16.04:

sudo apt-get install php7.0-xml

Edit: Restart apache2 to load the new package.

E_p
  • 3,088
  • 14
  • 28
Vladimir Korshunov
  • 3,020
  • 2
  • 18
  • 25
12

On Ubuntu, you got the following message during the upgrade of PHP7:

php7.0 (7.0.3-6) unstable; urgency=medium

  * Several extensions have been split into separate extension packages:
   - php-dba - Database (dbm-style) Abstraction Layer
   - php-mbstring - Multibyte String
   - php-soap - SOAP
   - php-xml - DOM, SimpleXML, WDDX, XML, XMLReader and XMLWriter
   - php-zip - Zip
  * The new packages are not installed automatically, so you will need to
    install them by hand, if you use the functions in those modules.
  * Most modules that have been builtin before are now included in
    php7.0-common package and they are enabled by default for your
    convenience.  You can disable unneede modules via phpdismod tool.

 -- Ondřej Surý <ondrej@debian.org>  Mon, 22 Feb 2016 12:37:09 +0100

so it's one of those packages that's causing the problem. In this case, it's php-xml.

Note the

The new packages are not installed automatically

You can fix that by

sudo apt-get install php-xml
sudo apache2ctl graceful
Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
  • In case anyone encounters mb_regex_encoding not found in php7.0, install php-mbstring works for me. – cwhsu Oct 07 '16 at 15:35
4

If you are on CentOS, following command should work:

yum install php-xml
Waqar Alamgir
  • 8,752
  • 4
  • 25
  • 35
4

As the top voted answer did not work for me i found yet another package for php7 which (obvious by its name) fixed it for me

sudo apt-get install php7.0-mbstring
Guenther Schmitz
  • 1,645
  • 1
  • 7
  • 21
3

In Ubuntu 16.04 LTS, with php 5.6, try:

sudo apt-get install php5.6-xml
sudo service apache2 restart
1

The utf8_decode and utf8_encode functions are accessible in php 7:

function.utf8-encode - manual - php

This is because the php-xml package is missing in your php installation.

If your server is running Mandrake, enter "urpmi php-xml".

If your server is running EASYPHP on Windows, click on the EASYPHP logo to activate the php-xml module.

On debian or ubuntu try :

apt-get install php7.0-xmlrpc
Vladimir Korshunov
  • 3,020
  • 2
  • 18
  • 25
DevLoots
  • 746
  • 6
  • 19
-1

I encountered the same problem and for me,

sudo apt-get install php-patchwork-utf8

and restarting the apache2 server solved the problem (on Ubuntu 16.04 LTS).